gpt4 book ai didi

c++ - 一个 "hack"来获取 float 模板参数工作编译但在 g++ 和 clang 上出现段错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:28 26 4
gpt4 key购买 nike

我知道why I can't use float as template parameter以及如何设置模板类的 static const float 成员,这要归功于一对分子/分母。但我正在尝试另一个基于 reinterpret_cast 的“hack”,以从其 IEEE754 十六进制书写中“emule” float 模板参数。

这是一小段代码:

#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)为什么会出现段错误?首先如何实现我想要的?

最佳答案

您的代码将 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);

关于c++ - 一个 "hack"来获取 float 模板参数工作编译但在 g++ 和 clang 上出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22921893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com