我想寫一個簡單的編譯時間尺寸分析庫。我想創建一個編譯選項來刪除庫中的所有內容,而無需更改代碼。所以基本上我做了我自己的基本類型的版本,並且想要將它們替換爲實際的基本類型(如果選擇該選項的話)。用原始類型替換模板類
這個代碼的最小工作示例
#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;
}
當我刪除了#define DEBUG
線我得到的編譯錯誤
Error C2988 unrecognizable template declaration/definition 59
是否有替換的Int32
任何模板版本有道具有原始類型的代碼?
「沒有工作」*什麼都不起作用?你得到了什麼錯誤?* –
並且不要橢圓化整個基本展示...... –
但是......'Int32'是一個模板類型? Int32的'Dims'是什麼?如果'Int32'是一個簡單的(無模板)類型,那麼'使用Int32 = std :: int32_t'呢? – max66