"Взлом", чтобы получить работающий компилятор с параметром шаблона с плавающей точкой, но с ошибками на g++ и clang

Я знаю, почему я не могу использовать float в качестве параметра шаблона и как установить статический член const float класса шаблона благодаря паре числитель / знаменатель. Но я пробовал другой "взлом", основанный на reinterpret_cast, чтобы "эмулировать" параметры плавающего шаблона из его шестнадцатеричной записи IEEE754.

Вот маленький кусочек кода:

#include <iostream>
#include <cstdint>

template <uint32_t T>
struct MyStruct
{
    static const float value;
};

template <uint32_t T>
const float MyStruct<T>::value = *reinterpret_cast<float*>(T);

int main()
{
    typedef MyStruct<0x40490fdb> Test;
    std::cout << Test::value << std::endl;
    return 0;
}

Я компилирую это...

g++ -Wall -pedantic main.cpp -std=c++0x -g

без предупреждений.

И это сегфо...

brugelca@artemis:~/workspace/draft$ ./a.out 
Segmentation fault (core dumped)

Вот вывод valgrind:

brugelca@artemis:~/workspace/draft$ valgrind ./a.out
==10871== Memcheck, a memory error detector
==10871== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==10871== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==10871== Command: ./a.out
==10871== 
==10871== Invalid read of size 4
==10871==    at 0x4008B5: __static_initialization_and_destruction_0(int, int) (main.cpp:11)
==10871==    by 0x4008D1: _GLOBAL__sub_I_main (main.cpp:18)
==10871==    by 0x40093C: __libc_csu_init (in /home/brugelca/workspace/draft/a.out)
==10871==    by 0x5159D74: (below main) (libc-start.c:219)
==10871==  Address 0x40490fdb is not stack'd, malloc'd or (recently) free'd
==10871== 
==10871== 
==10871== Process terminating with default action of signal 11 (SIGSEGV)
==10871==  Access not within mapped region at address 0x40490FDB
==10871==    at 0x4008B5: __static_initialization_and_destruction_0(int, int) (main.cpp:11)
==10871==    by 0x4008D1: _GLOBAL__sub_I_main (main.cpp:18)
==10871==    by 0x40093C: __libc_csu_init (in /home/brugelca/workspace/draft/a.out)
==10871==    by 0x5159D74: (below main) (libc-start.c:219)
==10871==  If you believe this happened as a result of a stack
==10871==  overflow in your program's main thread (unlikely but
==10871==  possible), you can try to increase the size of the
==10871==  main thread stack using the --main-stacksize= flag.
==10871==  The main thread stack size used in this run was 8388608.
==10871== 
==10871== HEAP SUMMARY:
==10871==     in use at exit: 0 bytes in 0 blocks
==10871==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==10871== 
==10871== All heap blocks were freed -- no leaks are possible
==10871== 
==10871== For counts of detected and suppressed errors, rerun with: -v
==10871== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

Должен ли этот код скомпилироваться? (Я был немного удивлен, что и clang, и g++ допускают reinterpret_cast) Почему это происходит с ошибками? Как добиться того, чего я хотел в первую очередь?

2 ответа

Решение

Ваш код переосмысливает 0x40490fdb как указатель на float, а не как шестнадцатеричное значение float, Отсюда и segfault.

Попробуйте следующее:

constexpr float uint32_to_float(uint32_t val) {
  return *reinterpret_cast<float*>(&val);
}

template <uint32_t T>
const float MyStruct<T>::value = uint32_to_float(T);

Сегфоут в вопросе была вульгарной ошибкой, но вопрос остается. Как передать float в формате ieee754 в качестве аргумента шаблона?

С ++11 способ

Это решение было предоставлено NPE (изменено, но не проверено):

#include <iostream>
#include <cstdint>

template <uint32_t T>
struct MyStruct
{
    static const float value;
}; 

constexpr float uint32_to_float(uint32_t val) {
    return reinterpret_cast<float&>(val);
}

template <uint32_t T>
const float MyStruct<T>::value = uint32_to_float(T);

int main()
{
    const uint32_t pi = 0x40490fdb;
    std::cout << MyStruct<pi>::value << std::endl;
}

Старомодный способ

Относительно маленький, я горжусь этим!:-)

#include <iostream>
#include <stdio.h>

template <uint32_t T>
union MyStruct
{
    static const uint32_t int_val = T;
    static const float    flt_val;
};

template <uint32_t T>
const float MyStruct<T>::flt_val = reinterpret_cast<const float&>(MyStruct<T>::int_val);

int main()
{
    const uint32_t pi = 0x40490fdb;
    std::cout << MyStruct<pi>::flt_val << std::endl;
    return 0;
}
Другие вопросы по тегам