为什么非const的std ::数组:: operator []的不constexpr?

编程入门 行业动态 更新时间:2024-10-10 09:19:29
本文介绍了为什么非const的std ::数组:: operator []的不constexpr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图填补编译时一个二维数组与给定的功能。这里是我的code:

I'm trying to fill a 2D array on compile time with a given function. Here is my code:

template<int H, int W> struct Table { int data[H][W]; //std::array<std::array<int, H>, W> data; // This does not work constexpr Table() : data{} { for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) data[i][j] = i * 10 + j; // This does not work with std::array } }; constexpr Table<3, 5> table; // I have table.data properly populated at compile time

它工作得很好, table.data 是正确填充在编译时。

It works just fine, table.data is properly populated at compile time.

但是,如果我改变普通二维数组 INT [H] [W] 与的std ::阵列&LT;的std ::阵列&LT; INT, H&GT ;, W&GT; ,我在循环体的错误:

However, if I change plain 2D array int[H][W] with std::array<std::array<int, H>, W>, I have an error in the loop body:

error: call to non-constexpr function 'std::array<_Tp, _Nm>::value_type& std::array<_Tp, _Nm>::operator[](std::array<_Tp, _Nm>::size_type) [with _Tp = int; long unsigned int _Nm = 3ul; std::array<_Tp, _Nm>::reference = int&; std::array<_Tp, _Nm>::value_type = int; std::array<_Tp, _Nm>::size_type = long unsigned int]' data[i][j] = i * 10 + j; ^ Compilation failed

显然,我试图调用的std ::数组::运算符[] ,这是不是 constexpr 。现在的问题是,为什么它不是 constexpr ?如果C ++ 14允许我们修改 constexpr 的范围,这是为什么不通过的std ::阵列支持?

Obviously, I'm trying to call non-const overload of std::array::operator[], which is not constexpr. The question is, why it is not constexpr? If C++14 allows us to modify variables declared in constexpr scope, why this is not supported by std::array?

我常想,的std ::阵列就像普通数组,只有更好。但这里有一个例子,在那里我可以使用普通阵列,但不能使用的std ::阵列。

I used to think that std::array is just like plain array, only better. But here is an example, where I can use plain array, but cannot use std::array.

推荐答案

好吧,这的确是标准的监督。甚至有存在解决这个问题的建议:www.open-std/jtc1/sc22/wg21/docs/papers/2015/p0107r0.pdf

Ok, it is indeed an oversight in the standard. There even exists a proposal to fix this: www.open-std/jtc1/sc22/wg21/docs/papers/2015/p0107r0.pdf

[N3598]去掉constexpr成员函数为const的隐性标记。然而,  的std ::阵成员函数这一变化后不重新,导致一个令人惊讶的缺乏  对中的std ::阵列接口constexpr支持。本文通过添加修复了这个疏漏  constexpr到的std ::阵列的成员函数,可以用最小量的支持它  的工作。

[N3598] removed the implicit marking of constexpr member functions as const. However, the member functions of std::array were not revisited after this change, leading to a surprising lack of support for constexpr in std::array’s interface. This paper fixes this omission by adding constexpr to the member functions of std::array that can support it with a minimal amount of work.

更多推荐

为什么非const的std ::数组:: operator []的不constexpr?

本文发布于:2023-11-24 16:58:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   std   const   operator   constexpr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!