c++ - 数组索引是否为 constexpr? GCC不一致?

标签 c++ arrays gcc c++11 constexpr

在他对我问题的回答中 Avoiding struct in variadic template function iavr 评论说“std::array::operator[] 仅在 C++14 中是 constexpr”。我的 这里的问题是确保 GCC 行为不一致,而这不是 我错误地理解了标准。

我正在探索使用一些模板元编程的不同方式 将二维数组初始化为 Pascal 三角形(外部为 0)。 在我在这里尝试的那个中,我想尽可能避免使用 模板结构,特别是 variadic 支持 if constexpr 函数 和数组。

赶时间的读者请注意:我将以下三段代码放在一起 完整性,但您不需要理解它们。


我使用以下两个非常标准的定义:

template <typename... Ts> struct Sequence {};
template<unsigned N, unsigned... Is> struct Range {
    typedef typename Range<N-1, N-1, Is...>::type type;
};
template<unsigned... Is> struct Range<0, Is...> {
    typedef Sequence<std::integral_constant<unsigned int, Is>...> type;
};

然后我有下面的模板 constexpr 函数,它给出了一行 三角形,计算下一个:

// nextline
template <typename... SeqTis, typename T, size_t Size>
constexpr std::array<T, Size>
nextline(Sequence<SeqTis...>, const typename std::array<T, Size> ar) {
  return { 1, (ar[SeqTis::value]+ar[SeqTis::value+1])... };
}
template <typename T, size_t Size>
constexpr std::array<T, Size>
nextline(const typename std::array<T, Size> ar) {
  return nextline(typename Range<Size-1>::type(), ar);
}

下面在部分初始化的数组末尾添加一个元素:

template <typename... SeqTis, typename T, size_t Size>
constexpr std::array<T, Size>
appendarray(Sequence<SeqTis...>, const typename std::array<T, Size> ar, const T el) {
  return { ar[SeqTis::value]..., el };
}
template <size_t Pos, typename T, size_t Size>
constexpr std::array<T, Size>
appendarray(const typename std::array<T, Size> ar, const T el) {
  return appendarray(typename Range<Pos>::type(), ar, el);
}

在这些代码中,我使用了数组索引,而且效果很好。你可以试试 与:

constexpr auto ar0 = std::array<int, 3> { 1,0,0 };
constexpr auto ar1 = nextline(ar0);
constexpr auto ar2 = appendarray<2>(ar1, 12);
for (auto i: ar2) std::cout << i << " "; // prints 1 1 12

但是当我尝试编译以下递归结构时:

template <typename T, size_t N>
using Ar2  = std::array<std::array<T, N+1>, N+1>;

template<typename T, size_t N, size_t l> struct Loop {
  constexpr static Ar2<T, N> next() {
    return appendarray<l>(Loop<T, N, l-1>::next(),
                  nextline(Loop<T, N, l-1>::next()[l-1]));
  }
};

template<typename T, size_t N> struct Loop<T, N, 0> {
  constexpr static Ar2<T, N> next() {
    return Ar2<T, N>({ {1, 0} });
  }
};

};

然后 GCC 提示

[...]
binom2.cpp:48:30: note: ‘static constexpr Ar2<T, N> Loop<T, N, l>::next() [with T = long long int; long unsigned int N = 10ul; long unsigned int l = 10ul; Ar2<T, N> = std::array<std::array<long long int, 11ul>, 11ul>]’ is not usable as a constexpr function because:

constexpr static Ar2<T, N> next() {
                              ^
binom2.cpp:50:38: 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 = std::array<long long int, 11ul>; long unsigned int _Nm = 11ul; std::array<_Tp, _Nm>::reference = std::array<long long int, 11ul>&; std::array<_Tp, _Nm>::value_type = std::array<long long int, 11ul>; std::array<_Tp, _Nm>::size_type = long unsigned int]’
  nextline(Loop<T, N, l-1>::next()[l-1]));

似乎有时 GCC 允许 constexpr 数组索引,有时它 没有。我错过了什么吗?

最佳答案

据我了解,

T& std::array<T,N>::operator[]

不是constexpr,而是

 constexpr const T& std::array<T,N>::operator[] const

是...您必须手动转换任何返回(非 const)std::array 和 (const std::array&) 的内容,以使其选择正确的运算符。

在对转换语法进行一些修改之后,看起来正确的语法是:

nextline(((const Ar2<T, N>&) Loop<T, N, l-1>::next())[l-1])

尽管您可能仍想研究 const_cast,因为常规转换会移除函数调用的右值特性。

关于c++ - 数组索引是否为 constexpr? GCC不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21781727/

相关文章:

c++ - 在 Eclipse CDT 中重构 C++

c++ - Kruskal 算法,Kattis 中的运行时错误

c - 指向 char 的指针数组的指针与指向 char 的指针的指针(或 char** argv 与 char* (*argv)[])

javascript - 提取值

c - 对 sqrt(geany) 的 undefined reference

gcc - make 和 gcc 有什么区别?

c++ - "if" "elseif" "else"语句有哪些好的做法

javascript - 具有 2 个数组的对象

c - 如何在 GCC 中编译 C 程序以在 WinDbg 中启用调试?

c# - 在没有枚举的情况下在 C# 中实现自己的数据类型?