问题描述
是否可以在编译时将项目附加到 std::initializer_list
?
我正在编写一个结构体,试图在 std::initializer_list
中收集来自 3rd 方库的一堆 const char*
名称,这些名称与来自与名称相同的库中的某些特定类型相关联,但是在代码上断开连接(例如,名称不是类型的一部分,它们甚至不在同一个命名空间中)。
这是我要写的struct
:
template < typename T,typename... Args >
struct S : S< T >,S< Args... >
{
static auto
get_names( )
{
// concatenation should go here
// return S< T >::get_names() + S< Args >::get_names()...
// or something similar
}
};
template <>
struct S< lib::types::SpecificTypeA >
{
static auto
get_names( )
{
// the name is a const char * coming from the same 3rd party lib
return { lib::names::name_of_specific_type_a };
}
};
我使用的是 C++17。
解决方法
据我所知,您不能在 C++ 编译时构造任何对象。
在程序启动之前不能构造任何对象。
我们通常在main()
的开头构造这些对象。它不会影响性能。