简体   繁体   中英

User-defined Literal Operator on Macros

How does one use a user-defined literal operator on a macro that expands to some literal expression?

eg:

std::string operator""_str(const char* sz, std::size_t len)
{
    return std::string(sz);
}

Where implementation is something like:

#define expr "expression"
auto str = expr _str;

Adjacent string literals are automatically concatenated ( [lex.ext]/8 ), so

auto str = expr ""_str;

would work.

You need another macro that performs token pasting:

#define CONCAT2(A, B) A##B
#define CONCAT(A, B) CONCAT2(A, B)
auto str = CONCAT(expr, _str);

Demo

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