std::array 无法自动初始化为特定的数据列表

huangapple go评论67阅读模式
英文:

std::array couldn't be auto initialized for a certain data list

问题

static constexpr auto k_strap4_function_setting = std::array{0xf0000000, 0x70000000};  // 编译错误
static constexpr auto k_strap4_function_setting = std::array{0xe0000000, 0x70000000};  // 编译错误
static constexpr auto k_strap4_function_setting = std::array{0xd0000000, 0x70000000};  // 编译错误
static constexpr auto k_strap4_function_setting = std::array{0xc0000000, 0x70000000};  // 编译错误
static constexpr auto k_strap4_function_setting = std::array{0xb0000000, 0x70000000};  // 编译错误
static constexpr auto k_strap4_function_setting = std::array{0xa0000000, 0x70000000};  // 编译错误
static constexpr auto k_strap4_function_setting = std::array{0x90000000, 0x70000000};  // 编译错误
static constexpr auto k_strap4_function_setting = std::array{0x80000000, 0x70000000};  // 编译错误
static constexpr auto k_strap4_function_setting = std::array{0x70000000, 0x70000000};  // 可以正常工作
static constexpr auto k_strap4_function_setting = std::array{0x60000000, 0x70000000};  // 可以正常工作
static constexpr auto k_strap4_function_setting = std::array{0x50000000, 0x70000000};  // 可以正常工作
static constexpr auto k_strap4_function_setting = std::array{0x40000000, 0x70000000};  // 可以正常工作

这里是错误日志:

错误: 对于 'array' 模板参数的推导没有可行的构造函数或推导指南
static constexpr auto k_strap4_function_setting = std::array{0xf0000000, 0x70000000};

唯一的区别是“编译错误”定义的最高位是 "1"。

这个错误有道理吗?
还是 std::array 的一个 bug。

英文:

std::array couldn't be auto initialized for a certain data list:

    static constexpr auto k_strap4_function_setting = std::array{0xf0000000, 0x70000000};  // compile error
    static constexpr auto k_strap4_function_setting = std::array{0xe0000000, 0x70000000};  // compile error
    static constexpr auto k_strap4_function_setting = std::array{0xd0000000, 0x70000000};  // compile error
    static constexpr auto k_strap4_function_setting = std::array{0xc0000000, 0x70000000};  // compile error
    static constexpr auto k_strap4_function_setting = std::array{0xb0000000, 0x70000000};  // compile error
    static constexpr auto k_strap4_function_setting = std::array{0xa0000000, 0x70000000};  // compile error
    static constexpr auto k_strap4_function_setting = std::array{0x90000000, 0x70000000};  // compile error
    static constexpr auto k_strap4_function_setting = std::array{0x80000000, 0x70000000};  // compile error
    static constexpr auto k_strap4_function_setting = std::array{0x70000000, 0x70000000};  // good to work
    static constexpr auto k_strap4_function_setting = std::array{0x60000000, 0x70000000};  // good to work
    static constexpr auto k_strap4_function_setting = std::array{0x50000000, 0x70000000};  // good to work
    static constexpr auto k_strap4_function_setting = std::array{0x40000000, 0x70000000};  // good to work

here is the error log:

error: no viable constructor or deduction guide for deduction of template arguments of 'array'
    static constexpr auto k_strap4_function_setting = std::array{0xf0000000, 0x70000000};

The only difference is the highest bit is "1" for the "compile error" define.

Does that error make sense?
Or is a bug for std::array.

答案1

得分: 1

十六进制整数文字的类型,没有任何后缀,是以下列表中能够表示文字确切值的第一个:

int
unsigned int
long
unsigned long
long long
unsigned long long

在一个具有32位宽度的int的系统上,int可表示的最大值为0x7fffffffunsigned int可表示的最大值为0xffffffff。因此,从0x7fffffff0x80000000之间的所有文字都具有int类型,而从0x800000000xffffffff之间的文字都具有unsigned int类型。

对于std::array的推导指南是这样指定的,如果初始化器的所有元素类型不相同,它将失败。这就是为什么你的一些示例失败的原因。

要么明确指定数组的元素类型,例如std::array<unsigned int>而不是std::array,这样其他类型的初始化器将被隐式转换为指定的元素类型,要么在文字中添加U(或u)后缀,以始终强制它们选择上述列表中的无符号类型。

英文:

The type of a hexadecimal integer literal without any suffix is the first out of the following list that is able to represent the exact value of the literal:

int
unsigned int
long
unsigned long
long long
unsigned long long

On a system with 32 bit wide int, the largest value representable by int is 0x7fffffff and the largest value representable by unsigned int is 0xffffffff. So all literals up to 0x7fffffff have type int and those from 0x80000000 to 0xffffffff have type unsigned int.

The deduction guide for std::array is specified in such a way that it will fail if not all elements of the initializer have the same type. And that is why some of your examples fail.

Either specify the element type of the array explicitly, e.g. std::array<unsigned int> instead of std::array, so that initializers with other types will be implicitly converted to the specified element type, or add U (or u) suffixes to your literals to force them always to choose only unsigned types from the list above.

huangapple
  • 本文由 发表于 2023年6月5日 09:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定