Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1898392
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:45:33+00:00 2026-05-17T06:45:33+00:00

Given an compile-time constant integer (an object, not a macro), can I combine it

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:45:33+00:00Added an answer on May 17, 2026 at 6:45 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that they are compile time, so they can't be generic, and must
Can someone point to an example of how to programmatically create & use Radio
I've been given a 2D matrix representing temperature points on the surface of a
Given a set of functions, such as: template<class A1> Void Go(A1 a); template<class A1,
Assume I have a static generic class. Its generic type parameters are not available
I have tried so many different strategies to get a usable noise function and
SHORT FORM(!) In response the Jon Skeet's comment, what I want C# to be
Well i have an adobe air app that captures screen and saves image on
I am wondering what is produced by the compiler when using non-virtual derivation: template<

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.