简体   繁体   English

宏字符串串联

[英]Macro string concatenation

I use macros to concatenate strings, such as: 我使用宏来连接字符串,例如:

#define STR1 "first"
#define STR2 "second"

#define STRCAT(A, B) A B

which having STRCAT(STR1 , STR2 ) produces "firstsecond" . 具有STRCAT(STR1 , STR2 )生成"firstsecond"

Somewhere else I have strings associated to enums in this way: 在其他地方,我以这种方式将字符串与枚举关联:

enum class MyEnum
{
    Value1,
    Value2
}
const char* MyEnumString[] =
{
    "Value1String",
    "Value2String"
}

Now the following does not work: 现在,以下内容不起作用:

STRCAT(STR1, MyEnumString[(int)MyEnum::Value1])

I was just wondering whether it possible to build a macro that concatenate a #define d string literal with a const char* ? 我只是想知道是否可以构建一个宏,以将#define d字符串文字与const char* Otherwise, I guess I'll do without macro, eg in this way (but maybe you have a better way): 否则,我想我将没有宏,例如以这种方式(但也许您有更好的方法):

std::string s = std::string(STR1) + MyEnumString[(int)MyEnum::Value1];

The macro works only on string literals, ie sequence of characters enclosed in double quotes. 该宏仅适用于字符串文字,即用双引号引起来的字符序列。 The reason the macro works is that C++ standard treats adjacent string literals like a single string literal. 宏起作用的原因是C ++标准将相邻的字符串文字视为单个字符串文字。 In other words, there is no difference to the compiler if you write 换句话说,如果您编写,则编译器没有区别

"Quick" "Brown" "Fox"

or 要么

"QuickBrownFox"

The concatenation is performed at compile time, before your program starts running. 在程序开始运行之前,在编译时执行串联。

Concatenation of const char* variables needs to happen at runtime, because character pointers (or any other pointers, for that matter) do not exist until the runtime. const char*变量的连接需要在运行时进行,因为字符指针(或其他任何指针)直到运行时才存在。 That is why you cannot do it with your CONCAT macro. 这就是为什么您不能使用CONCAT宏来做到这一点。 You can use std::string for concatenation, though - it is one of the easiest solutions to this problem. 不过,您可以使用std::string进行串联-这是解决此问题的最简单方法之一。

It's only working for char literals that they can be concatenated in this way: 只能以这种方式将它们串联起来的char 文字

 "A" "B"

This will not work for a pointer expression which you have in your sample, which expands to a statement like 这不适用于示例中包含的指针表达式,该表达式会扩展为以下语句

 "STR1" MyEnumString[(int)MyEnum::Value1];

As for your edit: 至于您的编辑:
Yes I would definitely go for your proposal 是的,我一定会接受你的建议

 std::string s = std::string(STR1) + MyEnumString[(int)MyEnum::Value1];

Your macro is pretty unnecessary, as it can only work with string literals of the same type. 您的宏是不必要的,因为它只能与相同类型的字符串文字一起使用。 Functionally it does nothing at all. 从功能上讲,它什么也不做。

std::string s = STRCAT("a", "b");

Is exactly the same as: 与以下内容完全相同:

std::string s = "a" "b";

So I feel that it's best to just not use the macro at all. 因此,我觉得最好根本不使用宏。 If you want a runtime string concatenating function, a more C++-canonical version is: 如果需要运行时字符串连接函数,则更C ++规范的版本是:

inline std::string string_concat(const std::string& a, const std::string& b)
{
    return a + b;
}

But again, it seems almost pointless to have this function when you can just do: 但是,再次提供此功能似乎几乎毫无意义:

std::string a = "a string";
std::string ab = a + "b string";

I can see limited use for a function like string_concat . 我可以看到像string_concat这样的函数的使用受到限制。 Maybe you want to work on arbitrary string types or automatic conversion between UTF-8 and UTF-16... 也许您想处理任意字符串类型或在UTF-8和UTF-16之间自动转换...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM