C++ 将模板类的静态常量成员放在GCC的特定部分中

C++ 将模板类的静态常量成员放在GCC的特定部分中,c++,gcc,clang,sections,C++,Gcc,Clang,Sections,对于自定义构建步骤,我需要将一些数据放入特定部分。在GCC和clang上,可以使用 uuu属性_uuu((.my_section”))const int variable_uin_uthis_usection=42 为了使创建更简单、更不容易出错,我创建了一个模板类: #include <cstdint> #ifdef _MSC_VER #define SECTION(x) __declspec(section x) #else #define SECTION(x)

对于自定义构建步骤,我需要将一些数据放入特定部分。在GCC和clang上,可以使用

uuu属性_uuu((.my_section”))const int variable_uin_uthis_usection=42

为了使创建更简单、更不容易出错,我创建了一个模板类:

#include <cstdint>

#ifdef _MSC_VER
    #define SECTION(x) __declspec(section x)
#else
    #define SECTION(x) __attribute__((section(x)))
#endif

#define DATA_SECTION SECTION(".my_data")

// the type to store in the .my_data section
struct hook_table_entry_t
{
    std::uint32_t game_addr, hook_addr;
};

template<std::uint32_t address, std::uint32_t function>
struct bind_hook_t
{
    DATA_SECTION static const hook_table_entry_t s_entry;
};
template<std::uint32_t address, std::uint32_t function>
const hook_table_entry_t bind_hook_t<address, function>::s_entry {address, function};

// instantiate template to create value
template struct bind_hook_t<0xffffffff, 0xf0f0f0f0>;
太好了!但在GCC 5和7中测试时,我得到了以下结论:

Section .rodata._ZN11bind_hook_tILj4294967295ELj4042322160EE7s_entryE:
0000 ffffffff f0f0f0f0                    ........        

有没有办法让GCC将数据也放入.my_数据?注意:当
bind\u hook\t
不是一个模板时,它可以工作,但这违背了它的全部目的。

这听起来可能很明显,但您是否收到了来自叮当的任何警告?@Asu没有使用-Wall-Wextra-Wpedantic的警告
Section .rodata._ZN11bind_hook_tILj4294967295ELj4042322160EE7s_entryE:
0000 ffffffff f0f0f0f0                    ........