2017-10-12 17 views
0

간단한 컴파일 타임 차원 분석 라이브러리를 작성하려고합니다. 코드를 변경하지 않고 라이브러리가하는 모든 것을 제거하는 컴파일 옵션을 만들고 싶습니다. 그래서 본질적으로 기본 형식의 자체 버전을 만든 후 해당 기본 형식이 선택되면 실제 형식으로 대체하려고합니다.기본 유형별 템플릿 클래스 교체

이 코드의 최소한의 작업 예를

#include <iostream> 
#include <stdint.h> 

#define DEBUG 
#ifdef DEBUG 
    template<int lenght, int time, int mass, int charge, int temperature, int amount, int intensity> 
    struct Dimensions { 
     static const int64_t LENGHT = lenght; 
     static const int64_t TIME = time; 
     static const int64_t MASS = mass; 
     static const int64_t CHARGE = charge; 
     static const int64_t TEMPERATURE = temperature; 
     static const int64_t AMOUNT = amount; 
     static const int64_t INTENSITY = intensity; 
    }; 

    typedef Dimensions< 0, 0, 0, 0, 0, 0, 0 > Adimensional; 
    typedef Dimensions< 1, 0, 0, 0, 0, 0, 0 > Length; 
    typedef Dimensions< 0, 1, 0, 0, 0, 0, 0 > Time; 

    template<typename Dims> class Int32 { 
    private: 
     int32_t m_value; 

    public: 
     inline Int32() : m_value(0) {} 

     inline Int32(int32_t value) : m_value(value) {} 

     inline int32_t value() { 
      return m_value; 
     } 
    }; 

    template<typename Dims> 
    Int32<Dims> inline operator+(Int32<Dims> &lhs, Int32<Dims> &rhs) { 
     return Int32<Dims>(lhs.value() + rhs.value()); 
    } 

    struct Unmatched_dimensions_between_operands; 

    template<typename DimsLhs, typename DimsRhs> 
    Unmatched_dimensions_between_operands inline operator+(Int32<DimsLhs> &lhs, Int32<DimsRhs> &rhs); 
#else 
    template<typename Dims> using Int32<Dims> = std::int32_t; 
#endif 

int main(int argc, char* argv[]) { 
    Int32<Time> a = 2; 
    Int32<Time> b = 5; 

    std::cout << (a + b).value() << "\n"; 
    return 0; 
} 

난에 Int32의 템플릿 버전을 대체 할 적절한 방법이 있나요 내가 컴파일 오류

Error C2988 unrecognizable template declaration/definition 59 

을 얻을 #define DEBUG 줄을 제거 할 때 원시 타입의 코드?

+2

"작동하지 않았 음"* 작동하지 않는 것이 있습니까? 어떤 오류가 발생 했습니까? * –

+0

그리고 필수 박람회 전체를 생략하지 마십시오 ... –

+0

하지만 ... 'Int32'는 템플릿 유형입니까? 'Int32'에'Dims'이란 무엇입니까? 'Int32'가 단순한 (템플릿이없는) 타입이라면,'Int32 = std :: int32_t'를 사용하면 어떨까요? – max66

답변

1

시도 :

template<typename Dims> using Int32 = std::int32_t; 

은 또한 당신이 어떻게 든 (템플릿 인수가 사용되지 않습니다으로하는 방법, 중요하지 않습니다) Time (아마도 AdimensionalLength)를 정의 할 필요가있다.

편집 : 사용자가 valueInt32에 액세스하면 해당 프로그램이 계속 실행되지 않으며 물론 std::int32_t에 없습니다. 그러나 나는 그것이 당신을 바른 길로 인도하기를 희망합니다.

+0

고마워요! 그것은 효과가 있었지만 실제로 작동하는 이유를 이해하지 못합니다. 왜'Int32'의 템플릿이 명시 적이지 않은가? – mbtg

+0

별칭 템플릿 정의는 다음과 같습니다. template using Alias ​​= Foo ; – Knoep

+0

귀하의 경우에는 단순히 템플릿 매개 변수를 사용하지 않고 있습니다. – Knoep