【问题标题】:Using static constexpr member array as pointer in template parameters在模板参数中使用静态 constexpr 成员数组作为指针
【发布时间】: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


【解决方案1】:

我想分享我刚刚在 open-std 中找到的答案和一个合规的解决方案

我们都知道我们不能将任何变量作为非类型传递。

您知道我们可以将 const 引用传递给我们想要的任何东西吗?!

所以解决方案是:

#include <array>

// When passing std::array<std::size_t, EltCount> (by value), i get the error:
// 'struct std::array<long long unsigned int, EltCount>' is not a valid type for a template non-type parameter

template <std::size_t EltCount, const std::array<std::size_t, EltCount>& Elts>
struct Foo {};

static constexpr std::array<std::size_t, 3> arr = {1, 2, 3};

int main()
{
    Foo<3, arr> foo;// WORKING

    return 0;
}

第一个问题的答案是:

N4296 报价

14.3.2 模板非类型参数 [temp.arg.nontype]

对于引用或指针类型的非类型模板参数, 常量表达式的值不应引用(或对于指针类型,不应是地址): (1.1) — 一个子对象 (1.8),

故事的寓意:我们可以用引用做我们想做的事,而不是用指针。

【讨论】:

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