c++ - 找不到用户定义的整数文字

标签 c++ c++11 gcc clang

我正在尝试创建一个用户定义的文字,但在使用它时收到一条错误消息。 海湾合作委员会说

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/

相关文章:

c++ - Int vs Double 汇编

c++ - 检查点是否在 vector 内

c++ - 当我不知道类型时如何将参数传递给函数

c++ - 将 shared_ptr<T> 转换为 shared_ptr<void>

c - GCC __builtin_constant_p 总是返回 0

android - 如何通过我的 PC 终端在我的 android 设备上编译和运行 C 程序。?

c++ - 使用数组填充对称矩阵

c++ - 将字符串分配给结构元素

c++ - 如何找到 vector 元素的乘积?

c++ - =默认忽略访问说明符?