Передайте float в качестве параметра шаблона (bis)
Как и прежде, я пытаюсь "эмулировать" параметры шаблона с плавающей запятой на основе его записи IEEE754 благодаря reinterpret_cast. Я использую компилятор MSVC 2005 (он не поддерживает C++11).
Вот код:
#include <iostream>
#ifdef _MSC_VER
typedef unsigned __int32 uint32_t;
#else
# include <stdint.h>
#endif
template <uint32_t T>
union Other
{
static const uint32_t i = T;
static const float x;
};
template <uint32_t T>
const float Other<T>::x = reinterpret_cast<const float&>(Other<T>::i);
union Test
{
static const float x;
static const uint32_t i;
};
const float Test::x = 3.141592f;
const uint32_t Test::i = reinterpret_cast<const uint32_t&>(Test::x);
int main()
{
std::cout << Other<0x40490fdb>::x << std::endl; //works
std::cout << Other<Test::i>::x << std::endl; //doesn't compile
return 0;
}
Как вы можете видеть, я не могу получить ieee754 записи с плавающей точкой во время компиляции. Это компилируется с другим компилятором? Если нет, то почему? Можно ли добиться этого преобразования во время компиляции без C++11?