【问题标题】:Template argument deduction with parameter list initialisation带参数列表初始化的模板参数推导
【发布时间】:2018-05-07 06:31:48
【问题描述】:

我一直在尝试创建一个类,该类表示数组的非拥有、多维视图(有点像 N 维 std::string_view),其中维数是“动态”变化的。即维度数量和维度大小与类无关,而是在访问元素时指定(通过operator())。以下代码总结了我正在寻找的功能:

#include <array>
#include <cstddef>

template<typename T>
struct array_view {

    T* _data;

    // Use of std::array here is not specific, I intend to use my own, but similar in functionality, indices class.
    template<std::size_t N>
    T& operator()(std::array<std::size_t, N> dimensions, std::array<std::size_t, N> indices) const
    {
        std::size_t offset = /* compute the simple offset */;

        return _data[offset];
    }

};

int main()
{
    int arr[3 * 4 * 5] = {0};

    array_view<int> view{arr};

    /* Access element 0. */
    // Should call array_view<int>::operator()<3>(std::array<std::size_t, 3>, std::array<std::size_t, 3>)
    view({5, 4, 3}, {0, 0, 0}) = 1;
}

但是这个fails to compile(忽略operator()中明显的语法错误)与

main.cpp: In function 'int main()':
main.cpp:28:27: error: no match for call to '(array_view<int>) (<brace-enclosed initializer list>, <brace-enclosed initializer list>)'
  view({5, 4, 3}, {0, 0, 0}) = 1;
                           ^
main.cpp:11:5: note: candidate: 'template<long unsigned int N> T& array_view<T>::operator()(std::array<long unsigned int, N>, std::array<long unsigned int, N>) const [with long unsigned int N = N; T = int]'
  T& operator()(std::array<std::size_t, N> dimensions, std::array<std::size_t, N> indices) const
     ^~~~~~~~
main.cpp:11:5: note:   template argument deduction/substitution failed:
main.cpp:28:27: note:   couldn't deduce template parameter 'N'
  view({5, 4, 3}, {0, 0, 0}) = 1;
                           ^

我不是模板实例化/演绎方面的专家。然而,在我看来,编译器试图从std::initializer_list&lt;int&gt; 参数中推断出N,但由于operator() 被声明为采用std::array&lt;std::size_t, N&gt; 参数而失败。因此编译失败。

执行another, far more simplified, experiment,显示类似结果:

template<typename T>
struct foo {
    T val;
};

struct bar {
    template<typename T>
    void operator()(foo<T>) {}
};

int main()
{
    bar b;
    b({1});
}

输出:

main.cpp: In function 'int main()':
main.cpp:14:7: error: no match for call to '(bar) (<brace-enclosed initializer list>)'
  b({1});
       ^
main.cpp:8:10: note: candidate: 'template<class T> void bar::operator()(foo<T>)'
     void operator()(foo<T>) {}
          ^~~~~~~~
main.cpp:8:10: note:   template argument deduction/substitution failed:
main.cpp:14:7: note:   couldn't deduce template parameter 'T'
  b({1});

似乎编译器甚至没有尝试将{1}(这是foo&lt;int&gt; 的有效初始化)转换为foo&lt;int&gt;,因为它在未能推断出函数模板参数后停止。

那么有什么方法可以实现我正在寻找的功能吗? 是否有一些我遗漏的新语法,或者可以替代的方法,或者根本不可能?

【问题讨论】:

    标签: c++ templates c++17 template-argument-deduction


    【解决方案1】:

    那么有什么方法可以实现我正在寻找的功能吗?是否有一些我遗漏的新语法,或者有相同功能的替代方法,或者根本不可能?

    显然您可以如下显式显示N 值

    view.operator()<3U>({{5U, 4U, 3U}}, {{0U, 0U, 0U}}) = 1;
    

    但我知道这是一个丑陋的解决方案。

    对于另一种方法...如果您可以接受放弃第二个数组(并使用第二个初始化列表调用运算符)并且如果您可以使用可变参数模板列表...大小可变参数列表的维度成为幸存数组的维度

    template <typename ... Ts>
    T & operator() (std::array<std::size_t, sizeof...(Ts)> const & dims,
                    Ts const & ... indices) const
     {
        std::size_t offset = 0 /* compute the simple offset */;
    
        return _data[offset];
     }
    

    你可以按如下方式使用它

    view({{5U, 4U, 3U}}, 0U, 0U, 0U) = 1;
    

    显然indices在运算符内部的使用可能更复杂,可能需要添加一些关于Ts...类型的检查(以验证它们都可以转换为std::size_t

    但我想您也可以将func() 方法定义为您原来的operator()

    template <std::size_t N>
    T & func (std::array<std::size_t, N> const & dims,
              std::array<std::size_t, N> const & inds) const
     {
       std::size_t offset = 0 /* compute the simple offset */;
    
       return _data[offset];
     }
    

    您可以通过operator() 拨打电话

    template <typename ... Ts>
    T & operator() (std::array<std::size_t, sizeof...(Ts)> const & dims,
                    Ts const & ... indices) const
     { return func(dims, {{ indices... }}); }
    

    以下是一个完整的工作示例

    #include <array>
    #include <cstddef>
    
    template <typename T>
    struct array_view
     {
       T * _data;
    
       template <std::size_t N>
       T & func (std::array<std::size_t, N> const & dims,
                 std::array<std::size_t, N> const & inds) const
        {
          std::size_t offset = 0 /* compute the simple offset */;
    
          return _data[offset];
        }
    
    
       template <typename ... Ts>
       T & operator() (std::array<std::size_t, sizeof...(Ts)> const & dims,
                       Ts const & ... indices) const
        { return func(dims, {{ indices... }}); }
    
     };
    
    int main ()
     {
       int arr[3 * 4 * 5] = {0};
    
       array_view<int> view{arr};
    
       view({{5U, 4U, 3U}}, 0U, 0U, 0U) = 1;
     }
    

    【讨论】:

      【解决方案2】:

      它无法编译的原因是{5, 4, 3} 和{0, 0, 0}(称为braced-init-lists)之类的东西不像一流的表达式。他们没有类型。在模板推导中,我们尝试将一个类型匹配到一个表达式 - 我们不能针对 braced-init-list 这样做。即使我们可以,braced-init-list 也不是任何类型的 std::array,所以这不匹配。这是我们需要额外的语言支持而我们只是没有的东西。

      有两个例外。

      最大的是std::initializer_list&lt;T&gt;。但这有运行时大小,在这种情况下我们不想要,因为您想强制 dimensions 和 indices 参数具有相同的大小(大概)。

      另一个是原始数组。您可以从花括号初始化列表中推断出T[N]。所以你可以这样写:

      template <typename D, typename I, std::size_t N>
      T& operator()(D const (&dimensions)[N], I const (&indices)[N]) const;
      

      这将让您编写view({5, 4, 3}, {0, 0, 0}),这将推断D 和I 为int 和N 为3。它还将正确地阻止 view({5, 4, 3}, {0}) 和 view({5}, {0, 0}) 编译。

      您可能想要添加额外的约束,D 和 I 是整数类型。

      【讨论】:

      • 我喜欢数组的想法,因为它几乎与我想要的功能相同。 但是,它会强制您使用完全相同的元素类型来调用它,这似乎是不必要的限制(在我最初失败的方法中,所有参数都将隐式转换为std::size_t)。我希望有std::tuple 样式std::initializer_list,但没有......(甚至不能有编译时std::initializer_list 大小,即使标准说基础数据存储为T const[N]。 ..)
      猜你喜欢
      • 2021-12-25
      • 2021-11-24
      • 2022-01-11
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多