C++ 如何将使用参数包和类型名的类作为函数(c+;+;)的输入参数

C++ 如何将使用参数包和类型名的类作为函数(c+;+;)的输入参数,c++,class,templates,C++,Class,Templates,我创建了一个类,它采用typename模板变量和参数包。在下一步中,我希望能够将该类的两个对象传递给我的函数 我的主要问题是,正确地传递模板参数和对象,以便能够使用我的函数。 我的类实现 //auto as template parameter is for non-type parameter(c++17) template <auto value> constexpr auto DIM = value; //constexpr on values in header files(

我创建了一个类,它采用
typename
模板变量和
参数包
。在下一步中,我希望能够将该类的两个对象传递给我的函数

我的主要问题是,正确地传递模板参数和对象,以便能够使用我的函数。 我的类实现

//auto as template parameter is for non-type parameter(c++17)
template <auto value> constexpr auto DIM = value;
//constexpr on values in header files(c++17)
inline constexpr auto const DIM3 = DIM <3>;
inline constexpr auto const DIM2 = DIM <2>;


enum Index : int {lower = 0, upper = 1};


template<int base, int exponent>
int constexpr pow(){
    if constexpr(exponent == 0){
        return 1;
    }else{
        return base * pow<base, exponent-1>();
    }
}
template<int Size, typename T>
struct Array{
    T array[Size];
    Array(const T * a){
        for(int i = 0; i < Size; i++){
            array[i] = a[i];
        }
    }
};



//auto as template parameter is for non-type parameters(c++17)
template<typename T = double, auto ...IndicesN>
class MatrixND{

private:
    const Array<pow<DIM3, sizeof...(IndicesN)>(), T> matrix;

public:
    MatrixND(const T * arr): matrix(arr){}

    template<auto ...args>
    auto constexpr getElement(){
    }
};
//自动作为模板参数用于非类型参数(c++17)
模板constexpr auto DIM=值;
//头文件中值的constexpr(c++17)
内联常量表达式自动常量DIM3=DIM;
内联常量表达式自动常量DIM2=DIM;
枚举索引:int{lower=0,upper=1};
模板
int constexpr pow(){
如果constexpr(指数==0){
返回1;
}否则{
返回基*pow();
}
}
模板
结构数组{
T数组[大小];
数组(常数T*a){
对于(int i=0;i
获取MatrixND对象的函数:

template<auto posT1, auto posT2, typename A, typename B>
auto constexpr function(const MatrixND<A> tensor1, const MatrixND<B> tensor2){

    return 0;
}
模板
自动常量表达式函数(常量矩阵和张量1,常量矩阵和张量2){
返回0;
}
我尝试了以下操作,但它会抛出一条错误消息“没有匹配的函数调用”:

const double arrayc[27]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27};
自动矩阵X1=新矩阵ND(arrayc);
函数(matrix1,matrix1);
错误消息:

error: no matching function for call to ‘function<1, 1, MatrixND<double, (Index)1, (Index)0, (Index)0>*, MatrixND<double, (Index)1, (Index)0, (Index)0>*>(MatrixND<double, (Index)1, (Index)0, (Index)0>*&, MatrixND<double, (Index)1, (Index)0, (Index)0>*&)’
     contraction<1,1, decltype(matrix1), decltype(matrix1)>(matrix1, matrix1);
错误:调用“函数(MatrixND*&,MatrixND*&)”时没有匹配的函数
收缩(matrix1,matrix1);

将函数调用为

function<1,1, decltype(matrix1), decltype(matrix1)>(matrix1, matrix1);
您可能还应该传递引用,而不是作为参数传递值


如果您喜欢上面的函数,您也不需要
A
B
类型的模板参数,这将由编译器推断:

function<1,1>(matrix1, matrix1);
函数(matrix1,matrix1);

将函数调用为

function<1,1, decltype(matrix1), decltype(matrix1)>(matrix1, matrix1);
您可能还应该传递引用,而不是作为参数传递值


如果您喜欢上面的函数,您也不需要
A
B
类型的模板参数,这将由编译器推断:

function<1,1>(matrix1, matrix1);
函数(matrix1,matrix1);

您需要将参数包添加到
函数的模板参数中。使用

template<auto posT1, auto posT2, typename A, typename B, auto ...AIndicesN, auto ...BIndicesN>
auto constexpr function(const MatrixND<A, AIndicesN...> tensor1, const MatrixND<B, BIndicesN...> tensor2){

    return 0;
}
模板
自动常量表达式函数(常量矩阵和张量1,常量矩阵和张量2){
返回0;
}
允许您调用函数,如

auto foo = function<1,1>(matrix1, matrix1);
auto foo=函数(matrix1,matrix1);

请注意,要使用您的代码编译此文件,您需要进行更改

auto matrix1 = new MatrixND<double, upper, lower, lower>(arrayc);
automatrix1=新矩阵nd(arrayc);

automatrix1=MatrixND(arrayc);

由于您实际上不需要指向
MatrixND
的指针,而是实际的
MatrixND
对象。

您需要将参数包添加到
函数的模板参数中。使用

template<auto posT1, auto posT2, typename A, typename B, auto ...AIndicesN, auto ...BIndicesN>
auto constexpr function(const MatrixND<A, AIndicesN...> tensor1, const MatrixND<B, BIndicesN...> tensor2){

    return 0;
}
模板
自动常量表达式函数(常量矩阵和张量1,常量矩阵和张量2){
返回0;
}
允许您调用函数,如

auto foo = function<1,1>(matrix1, matrix1);
auto foo=函数(matrix1,matrix1);

请注意,要使用您的代码编译此文件,您需要进行更改

auto matrix1 = new MatrixND<double, upper, lower, lower>(arrayc);
automatrix1=新矩阵nd(arrayc);

automatrix1=MatrixND(arrayc);

因为您实际上不需要指向
MatrixND
的指针,而是实际的
MatrixND
对象。

auto-constexpr函数(const a&tensor1,B const&tensor2){
?旁注:
pow
未声明
constexpr
(它包含参数)所以
const数组矩阵;
不会编译。我想你定义了自己的?@AndyG对不起,我忘了代码。我编辑了我的代码。
auto-constexpr函数(const A&tensor1,B const&tensor2){
?旁注:
pow
没有声明
constexpr
(它需要参数)所以
常量数组矩阵;
不会编译。我想你已经定义了你自己的?很抱歉,我忘记了代码。我编辑了我的代码。值得补充的是,当我使用
常量a张量1
时,可以使用类型特征和静态断言来确保
a
B
确实是
矩阵和
吗>常量B tensor2
我无法使用类的函数。例如tensor1.getElement()。有没有办法使用我的objects函数?@M.Mac为什么?这会给你带来什么问题?@Someprogrammerdude我很抱歉,它可以工作,但我的自动完成功能不起作用。值得补充的是,当我使用
常量a时,可以使用类型特征和静态断言来确保
a
B
确实是
矩阵和
tensor1
const B tensor2
我不能使用我的类的函数。例如tensor1.getElement()。有没有办法使用我的objects函数?@M.Mac为什么?这会给你带来什么问题?@Someprogrammerdude对不起,它能用,但我的自动完成功能不能用。