问题描述
我知道以及如何通过分子/分母对来设置模板类的静态const浮点成员。但我正在尝试另一个hack基于reinterpret_cast,从 IEEE754 十六进制写入emule浮动模板参数。
I know why I can't use float as template parameter and how to set a static const float member of template class thanks to a numerator/denominator couple. But I was trying another "hack" based on reinterpret_cast to "emule" float template parameters from its IEEE754 hexadecimal writing.
一段代码:
#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
没有警告。
并且它segfaulted ...
And it segfaulted...
brugelca@artemis:~/workspace/draft$ ./a.out
Segmentation fault (core dumped)
这里是valgrind输出:
Here is the valgrind output :
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)
? (我有点惊讶,cl和g + +允许reinterpret_cast)为什么它segfaulted?如何实现我想要的第一个地方?
Should this code compile ? (I was a bit surprised that both clang and g++ allow the reinterpret_cast) Why does it segfaulted ? How to achieve what I wanted in the first place ?
推荐答案
您的代码将重新解释 0x40490fdb
作为 float
,而不是 float
的十六进制值。因此,segfault。
Your code reinterprets 0x40490fdb
as a pointer to float
, not as the hex value of a float
. Hence the 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);
这篇关于“攻击”获得浮动模板参数工作编译,但在g ++和clang上出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!