简体   繁体   中英

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" .

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* ? 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. 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. That is why you cannot do it with your CONCAT macro. You can use std::string for concatenation, though - it is one of the easiest solutions to this problem.

It's only working for char literals that they can be concatenated in this way:

 "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:

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 . Maybe you want to work on arbitrary string types or automatic conversion between UTF-8 and UTF-16...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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