C++ 中新的用户定义文字概念暗示了字符串文字的一些非常有趣的用途,例如:
"Goodbye %s world"_fmt("cruel");
"Goodbye %s world"_fmt(123); // Error: arg 1 must be convertible to const char*
R"(point = \((\d+), (\d+)\))"_re; // Builds DFA at compile-time.
typedef table<
column<"CustId"_name , std::string>,
column<"FirstName"_name, std::string>,
column<"LastName"_name , std::string>,
column<"DOB"_name , date >
> Customer;
但是,当我在 gcc 中构建这些类型的结构时,例如:
template <char... Chars> Name<Chars...> operator "" _name() {
return Name<Chars...>();
}
auto a = 123_name; // OK
auto b = "abc"_name; // Error
我收到以下错误:
…unable to find string literal operator ‘operator"" _name’ with ‘const char [4]’, ‘long unsigned int’ arguments
通过阅读,我猜测从字符串文字派生的 UDL 无法使用可变参数模板形式。
- 实际上是不是使用可变参数模板形式无法解析字符串文字?
- 如果是这样,有没有人知道为什么这种有用的 UDL 形式被排除在标准之外?