간단한 컴파일 타임 차원 분석 라이브러리를 작성하려고합니다. 코드를 변경하지 않고 라이브러리가하는 모든 것을 제거하는 컴파일 옵션을 만들고 싶습니다. 그래서 본질적으로 기본 형식의 자체 버전을 만든 후 해당 기본 형식이 선택되면 실제 형식으로 대체하려고합니다.기본 유형별 템플릿 클래스 교체
이 코드의 최소한의 작업 예를
#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
줄을 제거 할 때 원시 타입의 코드?
"작동하지 않았 음"* 작동하지 않는 것이 있습니까? 어떤 오류가 발생 했습니까? * –
그리고 필수 박람회 전체를 생략하지 마십시오 ... –
하지만 ... 'Int32'는 템플릿 유형입니까? 'Int32'에'Dims'이란 무엇입니까? 'Int32'가 단순한 (템플릿이없는) 타입이라면,'Int32 = std :: int32_t'를 사용하면 어떨까요? – max66