2017-03-07 14 views
1

템플릿을 사용하여 수학적으로 벡터를 구현하려고합니다. 클래스에서 표준 벡터 상수를 정의하고 싶습니다. 나는 단순한 상수 (모든 0, 모든 것들)를 위해 그것을 관리했지만, 이제는 더 어려운 단위 벡터 (주어진 인덱스에서 하나의 구성 요소를 제외하고는 모두 0)를 정의하는 데 어려움을 겪고 있습니다.C++ 템플릿 템플릿 클래스의 정적 const 멤버 변수

아직 우아한 방법을 찾지 못했습니다.

#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 정적 const 멤버 템플릿을 정의하는 방법을 발견하지 않은 : 여기가 정의하고자하는 방법이다.

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

그러나 컴파일러 (clang & gcc가) 불평 : http://melpon.org/wandbox/permlink/AzbuATU1lbjXkksX

그것으로도 가능 : 여기

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 

이 테스트의 라이브 예입니다

나는이 시도 정적 const 템플릿 변수 멤버가 템플릿 클래스에 있습니까? 그리고 만약 그렇다면 어떻게?

그리고 아직 msUnit 템플릿의 초기화 도구를 제공하는 방법을 찾아야합니다. 두 개의 템플릿을 사용해야, 즉

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

를 하나 모두 다른 사람에게 n 번째 인수를 초기화 현재 일치하는 생성자가 없기 때문에 -

+1

내가 변수 템플릿 (따라서이 코멘트 아닌 대답은) 경험이없는,하지만 난 구문이 '수 템플릿 <부호에 기대 : 여기

당신은 라이브 데모 이동 tSeal, tReal> Vector :: msUnit ; – Angew

+0

초기화 프로그램의 경우 재귀가 필요할 것입니다. – Angew

+1

제안 된 정의가 실제로 아무것도 정의하지 않습니다. 템플릿을 실제로 정의하려면 실제 템플릿 매개 변수 즉,'tIndex','tSize' 및'tReal'을 제공해야합니다. –

답변

3

당신은 이미 당신이 당신의 msUnit을 decleare 수있는 방법을 방법을 발견 (제 생각에는 맞지 않습니다) 0으로, 당신은 당신 자신의 함수를 사용할 수 있습니다. 이미 클래스의 정적 멤버에 액세스, 당신은 또한 당신의 완전한 클래스에 액세스 할 수 있습니다, 그래서 당신은 단지

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)}; 

을 사용할 수 있도록 모든 것을 당신은 이미 그것을 가지고,하지만 당신은 주어진 몇 가지 다른 기능을 사용하여 벡터를 초기화로 수업 시간에. http://melpon.org/wandbox/permlink/5b7cgXTeqXZDoCRp

+0

안녕하세요, 나는 대답을 받아 들였지만 템플릿 고정 메서드로 전환하기 위해 템플릿 변수에 대해 C++ 14 확장을 사용할 필요가 없습니다. – Lagf