【发布时间】:2020-08-26 23:05:49
【问题描述】:
下面的 main.cpp 说明了这个问题:
#include <type_traits>
template <class T, std::size_t N>
struct Array
{
T data_[N];
};
template <const std::size_t* EltArray, std::size_t EltCount>
struct Foo
{
};
int main()
{
// SIDE NOTE if arr is not declared static: the address of 'arr' is not a valid template argument
// because it does not have static storage duration
static constexpr std::size_t arr[3] = {1, 2, 3};
Foo<arr, 3> foo;// WORKING
static constexpr Array<std::size_t, 3> arr2 = {1, 2, 3};
static constexpr const std::size_t* arr2_ptr = arr2.data_;
Foo<arr2_ptr, 3> foo2;// ERROR:
// 'arr2_ptr' is not a valid template argument of type 'const size_t*'
// {aka 'const long long unsigned int*'} because
// 'arr2.Array<long long unsigned int, 3>::data_' is not a variable
static constexpr const std::size_t* test = std::integral_constant<const std::size_t*, arr2_ptr>{};// ERROR:
// 'arr2_ptr' is not a valid template argument of type 'const long long unsigned int*' because
// 'arr2.Array<long long unsigned int, 3>::data_' is not a variable
return 0;
}
我不明白为什么 arr2.data_ 不能像 arr 一样重用。谁能解释一下?
我正在使用 gcc:mingw-w64\x86_64-8.1.0-posix-sjlj-rt_v6-rev0
g++.exe -Wall -std=c++2a -fconcepts -O2
【问题讨论】:
-
我没有看到来自eel.is/c++draft/temp.arg.nontype 的任何阻塞,但由于所有clang/gcc/msvc 都拒绝它Demo,我应该错过一些东西。
-
arr2_ptr 编译但错误谈论 arr2::data_ 所以看起来 gcc 是懒惰的。我得到与 std::array 相同的错误
标签: c++ templates static constexpr non-type