ReAl (03.01.2012 21:27, просмотров: 143) ответил Скрипач на Завести один enum для всех, использованных в проекте, единиц измерения конечно же выход. По примеру LONWORKS их нужно не так уж много. Спасибо, подумаем :)
Не, без разведения enum-ом они одинаковые, пускает x = a; Мне самому еще с шаблонами разбираться и разбираться. Так что как-то так (макрос лень было лепить, скопипастил и отредактировал). #include <iostream>
enum data_id_t { PRESSURE, TIME_10MS, TIME_1S };
template <typename T, data_id_t t> class strict
{
public:
T val;
explicit strict() : val() { }
explicit strict(T init_val) : val(init_val) { }
strict operator+(const strict b){ return strict(val + b.val);}
strict operator-(const strict b){ return strict(val - b.val);}
strict operator*(const strict b){ return strict(val * b.val);}
strict operator/(const strict b){ return strict(val / b.val);}
};
template <data_id_t t> struct data_type_t;
template<> struct data_type_t<PRESSURE>
{
static const char* name;
typedef strict<int,PRESSURE> value_t;
enum { type = PRESSURE, min = 200, max = 500 };
};
const char * data_type_t<PRESSURE>::name = "Pressure";
template<> struct data_type_t<TIME_10MS>
{
static const char* name;
typedef strict<int,TIME_10MS> value_t;
enum { type = TIME_10MS, min = 2, max = 1000 };
};
const char * data_type_t<TIME_10MS>::name = "Time 10ms";
typedef data_type_t<PRESSURE> pressure_t;
typedef data_type_t<TIME_10MS> time_10ms_t;
pressure_t::value_t a, b(1), c(3);
time_10ms_t::value_t x;
int main(void){
a = b + c;
// a = x; // не пускает
// ну val-то надо в private, а то всё лесом
std::cout << a.val << std::endl;
std::cout << pressure_t::name << "(" << pressure_t::type << ") = "
<< pressure_t::min << "..." << pressure_t::max << std::endl;
}