Re: How do you create and use an ostringstream in an initialisation list?
On Oct 12, 11:52 am, Adam Nielsen <adam.niel...@remove.this.uq.edu.au>
BTW: what problem is this strange hierarchy of Print-classes supposed =
solve? It looks as though you chose a very very roundabout way to print
some numbers. Why?
I was wondering about that myself.
It was just an example to demonstrate the problem. It has cropped up
twice in my code - once when I have a base exception class that takes an
error message, and then that's inherited by another class that handles
database errors - but since I want to include various additional info in
the error message that gets logged (e.g. database IDs) I have to cram
all these into a string to pass it on to the base class.
Great minds think alike---that's the exact case where I use the
static function to generate the string. (My exceptions aren't
from a database, but the exception constructor does need to
format a string from various other information, and use it to
initialize the base std::runtime_error class.)
The second time it came up someone may have some advice (even
if it is a bit off topic, as it's probably compiler specific.)
To assist with debugging I want to pass the source filename
and the line number to the object constructor when throwing an
exception, as these particular exceptions should only be
thrown when there's a coding error (e.g. trying to read an
integer out of the database into a string.)
That's an interesting example. Traditionally, I'd say that
coding errors should be handled via assert, rather than an
exception. But I think you've found an exception to that
rule---the coding error could also be in the data base schema,
and you probably don't want to bring the application down
because of that.
where the exception was thrown from without having to run through a
debugger will make tracking down the problem that much easier
(especially in the hopefully unlikely event that it gets thrown when the
system is in production.)
I can use __FILE__ to pass the current filename in as a const char *,
but using __LINE__ passes the line number as an integer (hence the need
for an ostringstream to convert it to a string.) Using
__STRING(__LINE__) doesn't work (it expands to "__LINE__"), so using
string streams was the only way of doing this that came to mind.
You can use a double indirection in the macros:
#define S1( s ) # s
#define S( s ) S1( s )
#define HERE __FILE__ ":" S( __LINE__ )
std::cout << HERE << std::endl ;
But I still find the ostringstream more flexible with regards to
its formatting possibilities.
James Kanze (GABI Software) email:firstname.lastname@example.org
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34