为类模板参数生成唯一asm字符串字面量的实现问询
解决方案1:利用__COUNTER__宏+模板参数(简单可靠)
这个方法通过给Foo模板增加唯一数字参数,结合预处理宏__COUNTER__生成每个实例的唯一标签,完全符合「前缀+唯一数字后缀」的需求。
首先修改Foo的模板定义,新增非类型模板参数Id:
#include <algorithm> #include <utility> template <size_t n> struct StringLiteral { constexpr StringLiteral(const char (&str)[n]) { std::copy_n(str, n, value); } char value[n]; static constexpr size_t len = n; }; // 新增Id参数用于生成唯一asm标签 template <StringLiteral text, size_t Id> class Foo { private: template <size_t... i> static const char* get(const std::index_sequence<i...>&) { // 用#Id将数字参数字符串化,拼接成唯一标签 __attribute__((section("<Some specific section>"), internal_linkage)) static const char t[text.len] asm ("foo_" #Id) = { text.value[i]... }; return t; } public: static const char* get() { return get(std::make_index_sequence<text.len>()); } }; // 定义宏自动传入__COUNTER__作为唯一Id #define FOO_INSTANCE(str) Foo<StringLiteral(str), __COUNTER__>::get()
调用时使用宏代替直接实例化Foo:
auto* one = FOO_INSTANCE("Instance One"); auto* two = FOO_INSTANCE("Instance Two"); auto* three = FOO_INSTANCE("Instance Three");
每个实例的asm标签会是foo_0、foo_1、foo_2(数字随__COUNTER__递增),确保唯一性。__COUNTER__是Clang支持的预处理宏,会在编译单元内每次调用时自动递增。
解决方案2:编译期字符串哈希(无需修改调用方式)
如果希望保持Foo<"xxx">::get()的原始调用形式,可以通过编译期哈希字符串内容生成唯一后缀,结合Clang的字符串拼接扩展实现。
首先实现编译期哈希与整数转字符串的辅助逻辑:
#include <algorithm> #include <utility> template <size_t n> struct StringLiteral { constexpr StringLiteral(const char (&str)[n]) { std::copy_n(str, n, value); } char value[n]; static constexpr size_t len = n; }; // 编译期字符串哈希(DJB2算法) constexpr size_t djb2_hash(const char* str, size_t hash = 5381) { return *str == '\0' ? hash : djb2_hash(str + 1, ((hash << 5) + hash) ^ static_cast<size_t>(*str)); } // 辅助模板:编译期将整数转换为字符串字面量 template <size_t N> struct IntToString; template <> struct IntToString<0> { static constexpr const char value[] = "0"; }; template <size_t N> struct IntToString { static constexpr char value[20] = {}; static_assert(N > 0, "N must be positive"); constexpr IntToString() { size_t num = N; char* ptr = const_cast<char*>(value); while (num > 0) { *ptr++ = '0' + (num % 10); num /= 10; } *ptr = '\0'; // 反转字符串 char* start = const_cast<char*>(value); char* end = ptr - 1; while (start < end) { char temp = *start; *start = *end; *end = temp; start++; end--; } } }; template <size_t N> constexpr char IntToString<N>::value[20]; template <StringLiteral text> class Foo { private: // 计算当前字符串的编译期哈希值 static constexpr size_t hash_val = djb2_hash(text.value); // 获取哈希值的字符串形式 static constexpr const char* hash_str = IntToString<hash_val>::value; template <size_t... i> static const char* get(const std::index_sequence<i...>&) { // 拼接前缀与哈希字符串作为asm标签 __attribute__((section("<Some specific section>"), internal_linkage)) static const char t[text.len] asm ("foo_" hash_str) = { text.value[i]... }; return t; } public: static const char* get() { return get(std::make_index_sequence<text.len>()); } };
调用方式保持原始形式:
auto* one = Foo<"Instance One">::get(); auto* two = Foo<"Instance Two">::get(); auto* three = Foo<"Instance Three">::get();
该方案通过编译期计算字符串哈希值并转换为字符串,作为asm标签的后缀。不同字符串的哈希值大概率唯一,普通场景下完全满足需求;若需绝对唯一,可替换为更复杂的编译期唯一ID生成逻辑。
内容的提问来源于stack exchange,提问作者PluginPenguin
相关产品推荐
相关产品推荐

