C++ A「;“黑客”;要获取浮点模板参数,工作编译,但在g++;叮当声

C++ A「;“黑客”;要获取浮点模板参数,工作编译,但在g++;叮当声,c++,templates,c++11,reinterpret-cast,C++,Templates,C++11,Reinterpret Cast,由于分子/分母偶,我知道如何设置模板类的静态常量浮点成员。但我正在尝试另一种“黑客”方法,它基于IEEE754的十六进制写入,重新解释“emule”浮点模板参数 下面是一小段代码: #include <iostream> #include <cstdint> template <uint32_t T> struct MyStruct { static const float value; }; template <uint32_t T>

由于分子/分母偶,我知道如何设置模板类的静态常量浮点成员。但我正在尝试另一种“黑客”方法,它基于IEEE754的十六进制写入,重新解释“emule”浮点模板参数

下面是一小段代码:

#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;
}
没有警告

而且它是

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))为什么它会出错?如何实现我最初想要的

您的代码将
0x40490fdb
重新解释为指向
float
的指针,而不是
float
的十六进制值。因此出现了断层

请尝试以下操作:

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);
constexpr float uint32\u到\u float(uint32\u t val){
返回*重新解释强制转换(&val);
}
模板
常量浮点MyStruct::value=uint32到浮点(T);

问题中的错误是一个粗俗的错误,但问题仍然存在。如何将ieee754格式的浮点作为模板参数传递

C++11方式 该溶液由NPE提供(经修改但未经测试):

#包括
#包括
模板
结构MyStruct
{
静态常量浮点值;
}; 
constexpr浮点值uint32到uint32浮点值(uint32值){
返回重新解释(val);
}
模板
常量浮点MyStruct::value=uint32到浮点(T);
int main()
{
常数uint32_t pi=0x40490fdb;

std::cout到底是什么..这是一个随机的十六进制地址吗?你应该猜一下。;-)我可以补充一点,没有警告并不意味着没有SEGFULTS。编译器很优秀,但他们无法预测所有人为错误:错误太多了。@CantChooseServerNames:答案就像pi一样简单。@matovitch:请参阅更新后的答案和建议的解决方法und.我想我会删除这个问题。首先是因为我为它感到羞愧,其次是因为我认为这对其他任何人都没有用处。如果没有constexpr(我认为它是c++11)?@matovitch:你当然可以自由删除任何问题。但是,这样做是对那些努力尝试帮助你的人的不尊重。@matovitch:这不是关于分数,而是关于尊重彼此的时间。顺便说一句,社区在迅速解决不合适的问题方面做得很好,到目前为止还不是一个单独的问题e投票结果接近。
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);
#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;
}