模板中的 C++ 模板静态常量成员变量 class

C++ template static const member variable in template class

我正在尝试使用模板实现矢量(在数学意义上)。我想在 class 中定义标准向量常量。我设法为简单的常量(全零,全一)做了这件事,但我现在正在努力定义更困难的单位向量(除了一个组件在给定索引处设置为一之外的所有零)。

我还没有找到一种优雅的方法来做到这一点。这是我想定义的方式:

#include <iostream>

template<unsigned int tSize, typename tReal>
class Vector {
public:

    template<unsigned int tIndex>
    static const Vector msUnit;

    inline Vector () {}

    template<typename...tTypes>
    inline Vector (tTypes...pVals) {
        set(mReals, pVals...);
    }

    inline tReal operator[] (unsigned int pIndex) {
        return mReals[pIndex];
    }

    inline const tReal operator[] (unsigned int pIndex) const {
        return mReals[pIndex];
    }

protected:

    template<typename tType>
    void set (tReal* pPtr, const tType pVal) {
        *pPtr = pVal;
    }

    template<typename tType, typename...tTypes>
    void set (tReal* pPtr, const tType pVal, const tTypes...pVals) {
        *pPtr = pVal;
        set(pPtr+1, pVals...);
    }

    tReal mReals [tSize];

};

int main() {

    Vector<3,double> lVec = Vector<3,double>::msUnit<2>;

    std::cout << "Vector: (" << lVec[0] << ", " << lVec[1] << ", " << lVec[2] << ")" << std::endl;

    return 0;
}

但是我还没有找到定义 msUnit static const 成员模板的方法。

我试过这个:

    template<unsigned int tIndex, unsigned int tSize, typename tReal>
    const Vector<tSize,tReal> Vector<tSize,tReal>::msUnit<tIndex>;

但是编译器(clang & gcc)抱怨:

prog.cc:43:48: error: nested name specifier 'Vector<tSize, tReal>::' for declaration does not refer into a class, class template or class template partial specialization
const Vector<tSize,tReal> Vector<tSize,tReal>::msUnit<tIndex>;
                          ~~~~~~~~~~~~~~~~~~~~~^
prog.cc:43:54: error: expected ';' at end of declaration
const Vector<tSize,tReal> Vector<tSize,tReal>::msUnit<tIndex>;
                                                     ^
                                                     ;
prog.cc:43:54: error: expected unqualified-id

这里是这个测试的一个实例:http://melpon.org/wandbox/permlink/AzbuATU1lbjXkksX

甚至可以在模板 classes 中使用静态常量模板变量成员吗?如果是的话怎么办?

而且我仍然需要找到一种方法来为 msUnit 模板提供初始值设定项。

您已经找到了清除 msUnit 的方法 - 您必须使用两个模板,即

template<unsigned tSize, typename tReal>
template<unsigned tIndex>
const Vector<tSize, tReal> Vector<tSize, tReal>::msUnit;

由于您目前没有匹配的构造函数来将第 n 个参数初始化为 1 并将所有其他参数初始化为零(我猜这没有意义),您可以只使用自己的函数。由于您已经访问了 class 的静态成员,因此您还可以访问完整的 class,因此您可以只使用

template<unsigned int tSize, typename tReal>
class Vector {
public:
    // ...

    template<unsigned int tIndex>
    static const Vector msUnit;

private:
    static const Vector<tSize, tReal> createUnitVector(unsigned tIndex) {
        Vector<tSize, tReal> v{};
        v.mReals[tIndex] = tReal(1);
        return v;
    }

    tReal mReals [tSize];
};

template<unsigned tSize, typename tReal>
template<unsigned tIndex>
const Vector<tSize, tReal> Vector<tSize, tReal>::msUnit{Vector::createUnitVector(tIndex)};

所以你已经拥有了一切,但是你用 class 中给出的一些其他函数初始化你的向量。

下面是现场演示:http://melpon.org/wandbox/permlink/5b7cgXTeqXZDoCRp