静态 const 成员变量的部分特化

partial specialization of a static const member variable

目前,我已经实现了一个模板 class Mat,它是第三方库矩阵 class 的包装器(T 是组件的类型:double、int 等。 ).现在,我希望实现一个 Tensor class 使用一个 Mat 用于存储,第二个 Mat 用于映射索引。

我将 Tensor class 模板化为 Tensor,其中 T 与 Mat class 相同,Order 和 Dim 是整数,分别是顺序(或等级)和维度(2或 3) 张量。 Sym 是对称的布尔标志。

Tensor class 将利用 Voigt 符号将高阶张量压缩到矩阵上(例如,3×3×3×3 张量可以映射到6×6 矩阵;这是通过将每对索引映射到单个索引来完成的)。在 Voigt 表示法中,可以将 3×3 张量映射到 6×1 向量(矩阵),但将张量中的 (0,0) 分量移动到向量中的 0 位置。 similar (1,1) -> 1, (2,2) -> 2, (1,2)-> 3, (0,2)-> 4, and (0,1)-> 5. 类似的规则退出 2×2 张量(它们映射到 3×1 矩阵)。

为此,我希望我的张量 class 拥有矩阵:

0 5 4
5 1 3
4 3 2

如果 Dim == 3 且 Sym == 真。非对称张量和二维张量都有相应的映射(共 4 个)。这些不依赖于其他模板参数(T 和 Order)。

因此,我应该在什么时候对它们进行专业化? (在这里,这个问题适用于任何拥有模板化 class 需要静态 const 成员来仅部分特化的人)。

我在这里检查了这个问题:Where to define static const member variables of a template class。但它没有讨论部分专业化。

到目前为止,我在同一头文件中有前向声明和 class 定义:

//cl_Tensor.hpp
namespace myNamespace
{
template< typename T, int Order, int Dim, bool Sym >
class Tensor;
}

template< typename T, int Order, int Dim, bool Sym >
class myNamespace::Tensor
{
protected:
    myNamespace::Mat< T> mMat; // storage
    static const myNamespace::Mat < uint > mTensorMap;

public:
// member functions and the like...
}

在我的 Tensor class 单元测试中,我可以输入:

template<> const moris::Mat< moris::uint> moris::Tensor< moris::real, 1, 2, true>::mTensorMap = { { 0, 2}, {2, 1} };
template<> const moris::Mat< moris::uint> moris::Tensor< moris::real, 1, 2, false>::mTensorMap = { { 0, 3}, {2, 1} };
template<> const moris::Mat< moris::uint> moris::Tensor< moris::real, 1, 3, true>::mTensorMap = { { 0, 5, 4}, {5, 1, 3}, {4, 3, 2} };
template<> const moris::Mat< moris::uint> moris::Tensor< moris::real, 1, 3, false>::mTensorMap = { { 0, 5, 4}, {8, 1, 3}, {7, 6, 2} };

问题是我必须为每个订单(1、2、3 和 4)执行此操作。如果我有其他类型的张量(这里,real 是 long double 的 typdef),我将有太多重复代码。

那我在哪里可以初始化地图?

您不能部分特化 class 模板的静态数据成员,但您可以像您在问题中所说的那样显式特化它们。但是没有什么可以阻止您将定义外包给其他函数模板:

namespace myNamespace {
    template< typename T, int Order, int Dim, bool Sym >
    const Tensor<T, Order, Dim, bool>::Mat<Dim> mTensorMap = 
        TensorMapCreator<T, Order, Dim, Sym>::makeMap();
}

然后只需创建一个带有静态成员函数 makeMap() 的 class 模板 TensorMapCreator 即可,您可以根据自己的具体需要对其进行部分专门化:

template <typename T, int Order, int Dim, bool Sym>
struct TensorMapCreator;

template <typename T, int Order, bool Sym>
struct TensorMapCreator<T, Order, 2, Sym> {
    static Mat<2> makeMap(); 
};

template <typename T, int Order, bool Sym>
struct TensorMapCreator<T, Order, 3, Sym> {
    static Mat<3> makeMap(); 
};

// etc.

我能够摆脱其他答案并提出以下解决方案。在Tensor头文件中,我只在class定义中声明了static const。之后,我使用 MapCreator class.

对成员初始化进行模板化
//Tensor.hpp
template< typename T, int Order, int Dim, bool Sym >
class space::Tensor
{
protected:
    Mat< T> mStorageMat;
    static const Mat < unsigned int > mTensorMap;
public:
    // ...
};

template< typename T, int Order, int Dim, bool Sym >
const space::Mat< unsigned int> space::Tensor<T, Order, Dim, Sym>::mTensorMatp = space::TensorMapCreator< Dim, Sym>::makeMap();

然后,TensorMapCreator class 仅针对我的成员变量所依赖的两个参数进行模板化:

//TensorMapCreator.hpp
namespace space {
    template< int Dim, bool Sym>
    class TensorMapCreator;

    class TensorMapCreator< 2, true >; // specialized forward
    class TensorMapCreator< 2, false>; // declarations
    class TensorMapCreator< 3, true >;
    class TensorMapCreator< 3, false>;
}

class space::TensorMapCreator< 2, true >
{
public:
    static
    space::Mat< unsigned int>
    makeMap()
    {
        // creates the correct map
    }
};
// 3 more specializations for the other
// combinations of dimmension and symmetry