我正在尝试创建一个用户定义的文字,但在使用它时收到一条错误消息。 海湾合作委员会说
unable to find numeric literal operator ‘operator""_uint’
当 clang 告诉我
error: no matching literal operator for call to 'operator""_uint' with argument of type 'unsigned long long' or 'const char *', and no matching literal operator template
我将代码缩减为以下 MCVE:
#include <cinttypes>
unsigned int operator"" _uint(char const *, std::size_t) { return 0; }
int main() {
return 1_uint;
}
如您在 ideone 上看到的那样,它给出了提到的错误.
最佳答案
您可以在 cppreference.com 上详细阅读文字运算符有多种不同的版本。
OP 中的那个专门用于字符串文字,不适用于整数。取而代之的是整数的“后备”版本,它只需要一个 const char*
而没有第二个 std::size_t
参数:
#include <cinttypes>
unsigned int operator"" _uint(char const *) { return 0; }
int main() {
return 1_uint;
}
适用于 ideone .
关于c++ - 找不到用户定义的整数文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48963135/