I'm trying to write a simple compile-time dimensional analysis library. I want to create a compile option to remove everything the library does without changing the code. So essentially I made my own version of the primitive types and want to replace them by the actual primitive types if that option is selected.
This a minimal working example of the code
#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;
}
When I remove the #define DEBUG
line I get the compile error
Error C2988 unrecognizable template declaration/definition 59
Is there a proper way to replace any template version of Int32
in the code with a primitive type?
Try:
template<typename Dims> using Int32 = std::int32_t;
Also you need to define Time
(and probably Adimensional
and Length
) somehow (doesn't matter how, as the template argument is never used).
Edit: Your programm still won't run, as your accessing a member value
of Int32
which of course is not present in std::int32_t
. However, I hope that puts you on the right track.