Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 实例化后向可变模板类型添加内容_C++_Variadic Templates_Stdvector - Fatal编程技术网

C++ 实例化后向可变模板类型添加内容

C++ 实例化后向可变模板类型添加内容,c++,variadic-templates,stdvector,C++,Variadic Templates,Stdvector,电流源 在代码的这一点上,我有一个可变模板类: template<typename ClassType, std::size_t... Args> class Matrix { private: DimensionPack<Args...> dp; public: Matrix<ClassType, Args...>(){} // Default // Public Access Members To Get Information

电流源

在代码的这一点上,我有一个可变模板类:

template<typename ClassType, std::size_t... Args>
class Matrix {
private:
    DimensionPack<Args...> dp;

public:
    Matrix<ClassType, Args...>(){} // Default

    // Public Access Members To Get Information From the DimensionPack which is based
    // Not On The Type But Based On The Amount & Values Of This Template's Variadic Parameters
    std::vector<unsigned int>& getDimensions() { return dp.dimensions;  }
    std::vector<unsigned int>& getEvenOrOdd() { return dp.even_or_odd; }
    const unsigned int getNumberOfDimensions() const { return dp.total_dimensions; }
    const unsigned int getTotalNumElements() const { return dp.total_elements; }

};
此时,在我的
矩阵
类中,我能够成功编译并获得预期的结果,例如在以下情况下:

使用示例:

Matrix<float, 1> mat1;   // Single Element Matrix - Considered A Scalar Type
Matrix<float, 1,1...> mat1...; // Again - Single Element Matrix - Considered  A Scalar Type
Matrix<float, n, m, 1, p> matNM1P; // Where n,m,p are > 1 makes that field of dimensionality flat such as a linear array or vector.
Matrix<float, 2,2> mat2x2; // creates a 2x2 Matrix with 4 elements of type float
Matrix<int, 3,3,3> mat3x3x3; // creates a 3x3x3 Volumetric Matrix with 27 elements of type int.
Matrix<type,n...> mat_multidimensional; // creates any higher order dimension of type.

  • 我应该只接受一个固定大小的向量吗 匹配元素总数并创建索引方案


  • 如果您知道编译时的大小,而不是使用
    std::vector,我应该使用嵌套
    s@W.F.大小来自模板的可变列表,该列表恰好是
    std::size\u t…
    模板实例化显示了大小,例如
    矩阵
    将为您提供2维内的MxN元素和
    Matrix
    将为您提供三维内的MxNxO元素。而且没有固定数量的维度,因此理论上它可以是100维矩阵,但由于数据类型和它们可以包含的值的限制,硬件不会支持它。因此,你知道编译时的大小。在这种情况下,使用数组将更合适…@W.F.ty对于建议,我必须尝试一下。
    Matrix<float, 1> mat1;   // Single Element Matrix - Considered A Scalar Type
    Matrix<float, 1,1...> mat1...; // Again - Single Element Matrix - Considered  A Scalar Type
    Matrix<float, n, m, 1, p> matNM1P; // Where n,m,p are > 1 makes that field of dimensionality flat such as a linear array or vector.
    Matrix<float, 2,2> mat2x2; // creates a 2x2 Matrix with 4 elements of type float
    Matrix<int, 3,3,3> mat3x3x3; // creates a 3x3x3 Volumetric Matrix with 27 elements of type int.
    Matrix<type,n...> mat_multidimensional; // creates any higher order dimension of type.
    
    template<typename type, std::size_t... dims>
    void testMatrix( Matrix<type, dims...> matrix ) {
        std::cout << "The Matrix Has " << matrix.getNumberOfDimensions() << " Total Dimensions:\n";
        std::cout << "The Dimensions Are:\n";
        for ( unsigned u = 0; u < matrix.getDimensions().size(); u++ ) {
            std::cout << matrix.getDimensions()[u] << " ";
        }
        std::cout << std::endl;
    
        std::cout << "The even and odd of each dimension are:\n";
        for ( unsigned u = 0; u < matrix.getEvenOrOdd().size(); u++ ) {
            std::cout << matrix.getEvenOrOdd()[u] << " ";
        }
        std::cout << std::endl;
    
        std::cout << "The Matrix Has " << matrix.getTotalNumElements() << " total elements.\n\n";
    }
    
    #include <Matrix.h>
    
    int main() {
        Matrix<float, 2, 3, 4> mat;
        testMatrix( mat );
    
        Matrix<int, 7, 9, 13, 15, 17> mat2;
        testMatrix( mat2 );
    
        Matrix<double, 255,255,255,255,255,255,255,255,255> mat9;
        testMatrix( mat9 );
    
        return 0;
    }
    
    Matrix<float, 4,4,4> mat4x4x4;  // A 3D Volumetric Matrix With 256 Elements
    Matrix<float, 2,3,5> mat2x3x5;  // A 3D Volumetric Matrix With 30 Elements
    Matrix<float, 5,7,8,10> mat5x7x8x10; // A 4D Volumetric Matrix With 2800 Elements