C++ 组合字符串文字和整数常量

C++ 组合字符串文字和整数常量,c++,concatenation,c-preprocessor,string-literals,stringification,C++,Concatenation,C Preprocessor,String Literals,Stringification,给定一个编译时常量整数(一个对象,而不是一个宏),我可以在编译时将它与一个字符串文字组合在一起,可能与预处理器组合在一起吗 例如,我可以通过将字符串文字彼此相邻来连接它们: bool do_stuff(std::string s); //... do_stuff("This error code is ridiculously long so I am going to split it onto " "two lines!"); 太好了!但如果我在混合中添加整数常量会怎么样

给定一个编译时常量整数(一个对象,而不是一个宏),我可以在编译时将它与一个字符串文字组合在一起,可能与预处理器组合在一起吗

例如,我可以通过将字符串文字彼此相邻来连接它们:

bool do_stuff(std::string s);
//...
do_stuff("This error code is ridiculously long so I am going to split it onto "
         "two lines!");
太好了!但如果我在混合中添加整数常量会怎么样:

const unsigned int BAD_EOF = 1;
const unsigned int BAD_FORMAT = 2;
const unsigned int FILE_END = 3;
是否可以使用预处理器以某种方式将其与字符串文本连接起来

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!");
如果不可能,我可以将常量字符串与字符串文字混合使用吗?也就是说,如果我的错误代码是字符串,而不是无符号


如果两者都不可能,那么最短、最干净的方法是什么来修补字符串文字和数字错误代码的混合?

如果BAD\u EOF是一个宏,您可以:

但事实并非如此(这几乎总是一件好事),因此您需要:

这就有了宏的所有缺点,再加上需要维护的更多,以获得可能适用于大多数应用程序的一点点性能。但是,如果您决定进行这种折衷,它必须是一个宏,因为预处理器无法访问值,即使它们是常量。

有什么问题:

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!");
如果要提取错误代码,可以执行以下操作:

#define BAD_EOF "1"

然后你可以像使用字符串文字一样使用BAD_EOF。

啊,是的。双宏非常重要。在我的例子中我忘记了一些东西。好节目,嗯。如果将#define用作整型,这可能会破坏大量代码
unsigned const BAD_EOF = 1;
#define BAD_EOF_STR "1"
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!");
#define BAD_EOF "1"