模板参数无效(唯一指针)

Template arguments are invalid (unique pointer)

我正在尝试创建一个可动态调整大小的 N-dimensional 数组 class。我的模板 class 具有签名 ndarray<typename data_type, int dimensions>。为了保存数组,我想使用一个唯一的指针 class 成员来保存一维类型为 std::vector<type_t>、二维类型为 std::vector<std::vector<type_t>>、三维类型为 std::vector<std::vector<std::vector<type_t>>> 的模板化向量。 ..

唯一指针将根据维数具有不同的签名。我想让 ndarray 的 class 构造函数设置唯一指针的签名并使用 new 来制作新向量,但我需要一个 auto 成员变量设置为无效的成员变量类型的向量。我目前的方法是使用一个模板函数,该函数 returns 数组需要的类型 object,template<typename data_type> auto dimension_helper(int dimensions),然后像这样 std::unique_ptr<decltype(<data_type>dimension_helper(dimens))> array 设置唯一的指针签名。这也不起作用,给我错误 template argument 1 is invalid and template argument 2 is invalid inside the unique pointer。

我该怎么做才能使我现有的代码正常工作,或者是否有更好的方法以类似的方式解决问题?

代码示例

header

#ifndef ND_H_
#define ND_H_
#include <cstring>
#include <vector>
#include <memory>


namespace NdArray{
    //use the following for dimension types 
    template<typename T>
    using d1 = std::vector<T>;
    template<typename T>
    using d2 = std::vector<d1<T>>;
    template<typename T>
    using d3 = std::vector<d2<T>>;
    template<typename T>
    using d4 = std::vector<d3<T>>;
    template<typename T>
    using d5 = std::vector<d4<T>>;

    template<typename data_type>
    auto dimension_helper(int dim);



    template<typename data_type,int dimensions>
    class ndarray{
            int dims;
            std::unique_ptr<std::vector<int>> xsub_spans;

        public:
            ndarray();
            ~ndarray();
            std::unique_ptr<decltype(<data_type>dimension_helper(dimensions))> array;

            template<typename dat_type, int dim>
            friend std::ostream& operator<<(std::ostream& , ndarray<dat_type, dim>&);
    };

}


#endif

到目前为止的定义

#include "nd.h" 
using namespace NdArray;

template<typename data_type, int dimensions>
ndarray<data_type, dimensions>::ndarray(){
    dims = dimensions;
    array = new <data_type>dimension_helper(dimensions); 
}

template<typename data_type>
auto dimension_helper(){
    switch (dims) {
        case 1 : {
            d1<data_type> type;
            return type;
            break;
        }
        case 2 : {
            d2<data_type> type;
            return type;
            break;
        }
        case 3 : {
            d3<data_type> type;
            return type;
            break;
        }
        case 4 : {
            d4<data_type> type;
            return type;
            break;
        }
        case 5 : {
            d5<data_type> type;
            return type;
            break;
        }
    }
}

模板参数列表位于模板名称之后,因此:

dimension_helper<data_type>(dimensions)

而不是

<data_type>dimension_helper(dimensions)

然而,你的"dimension helper"没有机会工作,因为switch语句是运行时分支,并且编译失败,因为return语句中的表达式类型不相等。相反,您可以按如下方式重写它:

template <typename T>
struct identity { using type = T; };    
template <typename T, std::size_t D>
struct dimension_helper : identity<std::vector<typename dimension_helper<T, D-1>::type>> {};
template <typename T>
struct dimension_helper<T, 0> : identity<T> {};
template <typename T, std::size_t D>
using dimension_helper_t = typename dimension_helper<T, D>::type;

并将您的指针声明为:

std::unique_ptr<dimension_helper_t<data_type, dimensions>> array;

DEMO