根据我的C++17(16.5.8 [over.literal])(草案)版本以及cppreference.com,C++17应该支持用户定义字符串字面值的模板化操作符。
具体来说:
然而,gcc和clang都正确地发出了警告,指出这是一种非标准扩展,但它们编译得正确。gcc和clang都声称完全支持C++17核心语言。
我做错了什么?我的初稿和cppreference.com不准确吗?我目前无法访问最终的标准版本。
具体来说:
template <char...>
double operator "" _pi() {
return 0.;
}
int main() {
"test"_pi;
}
然而,gcc和clang都对我大喊大叫:
// gcc -Wall -Wextra -pedantic -std=c++17
error: no matching function for call to 'operator""_pi<char, 't', 'e', 's', 't'>()'
7 | "test"_pi;
| ^~~~~~~~~
note: candidate: 'template<char ...<anonymous> > double operator""_pi()'
2 | double operator "" _pi() {
// clang -Wall -Wextra -pedantic -std=c++17
error: no matching literal operator for call to 'operator""_pi' with arguments of types 'const char *' and 'unsigned long', and no matching literal operator template
(在线演示)
他们似乎都想引导我使用以下模板,这个模板曾被短暂地认为是C++17的一部分,但据我了解并没有成为其中的一部分:
template <typename T, T...>
double operator "" _pi() {
return 0.;
}
然而,gcc和clang都正确地发出了警告,指出这是一种非标准扩展,但它们编译得正确。gcc和clang都声称完全支持C++17核心语言。
我做错了什么?我的初稿和cppreference.com不准确吗?我目前无法访问最终的标准版本。
使用情况
我的使用情况是要实现以下内容,这就是为什么我需要编译时信息的原因。
template <char... cs>
constexpr auto operator "" _a() {
return std::array<char, sizeof...(cs)>({cs...});
}
// "Hello"_a creates a std::array<char,5>