views:

118

answers:

2

Given an compile-time constant integer (an object, not a macro), can I combine it with a string literal at compile time, possibly with the preprocessor?

For example, I can concatenate string literals just by placing them adjacent to each other:

bool do_stuff(std::string s);
//...
do_stuff("This error code is ridiculously long so I am going to split it onto "
         "two lines!");

Great! But what if I add integer constants in the mix:

const unsigned int BAD_EOF = 1;
const unsigned int BAD_FORMAT = 2;
const unsigned int FILE_END = 3;

Is it possible to use the preprocessor to somehow concatenate this with the string literals?

do_stuff("My error code is #" BAD_EOF "! I encountered an unexpected EOF!\n"
         "This error code is ridiculously long so I am going to split it onto "
         "three lines!");

If that isn't possible, could I mix constant strings with string literals? I.e. if my error codes were strings, instead of unsigneds?

And if neither is possible, what is the shortest, cleanest way to patch together this mix of string literals and numeric error codes?

+8  A: 

If BAD_EOF was a macro, you could stringize it:

#define STRINGIZE_DETAIL_(v) #v
#define STRINGIZE(v) STRINGIZE_DETAIL_(v)

"My error code is #" STRINGIZE(BAD_EOF) "!"

But it's not (and that's just about always a good thing), so you need to format the string:

stringf("My error code is #%d!", BAD_EOF)

stringstream ss; ss << "My error code is #" << BAD_EOF << "!";
ss.str()

If this was a huge concern for you (it shouldn't be, definitely not at first), use a separate, specialized string for each constant:

unsigned const BAD_EOF = 1;
#define BAD_EOF_STR "1"

This has all the drawbacks of a macro plus more to screwup maintain for a tiny bit of performance that likely won't matter for most apps. However, if you decide on this trade-off, it has to be a macro because the preprocessor can't access values, even if they're const.

Roger Pate
Ah, yes. That double macro is important. Something I forgot in my example. Good show.
JoshD
why the double macro?
Chubsdad
@Chubsdad: http://codepad.org/DiAC35hl
Roger Pate
A: 

What's wrong with:

do_stuff(my_int_1,
     my_int_2,
     "My error code is #1 ! I encountered an unexpected EOF!\n"
     "This error code is ridiculously long so I am going to split it onto "
     "three lines!");

If you want to abstract the error codes, then you can do this:

#define BAD_EOF "1"

Then you can use BAD_EOF as if it were a string literal.

Alexander Rafferty
Hmm. This could break a lot of code if that #define was being used as an integral type
Chubsdad