【问题标题】:user-defined literals combined with an uint64_t argument用户定义的文字与 uint64_t 参数相结合
【发布时间】:2019-03-01 14:50:00
【问题描述】:

我刚刚偶然发现了以下用户定义的文字:

#include <cstdint>

constexpr auto operator""_G(uint64_t v) { return v * 1'000'000'000ULL; }

但是,这不能与 GNU 7.3.0 和 -std=c++14 一起编译。我收到“有无效的参数列表”错误。

根据https://en.cppreference.com/w/cpp/language/user_literal,唯一允许的无符号64位类型是unsigned long long int。但是,stdint.h 中的 uint64_t typedef 映射到 GCC 内置定义 __UINT64_TYPE__

#define __UINT64_TYPE__ long unsigned int;

这个定义是通过运行gcc -dM -E an_empty_file.c | grep "__UINT64_TYPE__"得到的

当然,将uint64_t 替换为unsigned long long int 可以避免编译错误。但是这两种类型在 LP64 数据模型上是相同的。

这不应该默认工作吗?

【问题讨论】:

  • static_assert(std::is_same_v&lt;uint64_t, unsigned long long&gt;); 会触发。

标签: c++ c++14 user-defined-literals


【解决方案1】:

这不应该默认工作吗?

不。该标准要求用户定义文字的类型为unsigned long long int[1]long unsigned int 不是一回事,它是它自己独特的类型。即使它们具有完全相同的属性,std::is_same_v&lt;unsigned long long int, long unsigned int&gt; 也是错误的。

如果您想为文字取整数,则必须使用 unsigned long long int 类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 2012-10-18
    • 2021-03-03
    • 2017-10-28
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多