Combining string literals and integer constants

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?

Answers


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.


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.


Need Your Help

REST request times out after making several database connections

performance rest database-connection restlet

I have a REST application that uses RESTlets. I am querying several databases to create a response with the queried data. The process of closing and opening database connections takes sometimes 20

What's the correct file extension for T4 template files?

.net t4

I have seen both .tt and .t4. What's the correct/canonical extension? Or does it not even matter?