如何从模板获取类型和值信息
How to get type and value information from a template
目前我有一个看起来像这样的模板类:
template<typename T, T value>
struct MyStruct{
/* Stuff */
};
为了实例化它,我目前做了这样的事情:
typedef MyStruct<int, 123> struct_def;
我想做这样的事情:
typedef typename MagicTemplate<123>::type struct_def;
这将解决MyStruct<int,123>
....
所以我的问题是我如何从模板参数中获取类型和值信息?
编辑:
如果该解决方案可以与指针、成员指针、函数指针等一起使用,那也很好......
例如:
struct OtherStruct{
int memberA;
long memberB;
void foo(void);
};
static OtherStruct instance;
//Expands to MyStruct<memberA OtherStruct::*, &OtherStruct::memberA>
typedef typename MagicTemplate<&OtherStruct::memberA>::type struct_defA;
//Expands to MyStruct<OtherStruct*,&instance>
typedef typename MagicTemplate<&instance>::type struct_defB;
//Expands to MyStruct<void (OtherStruct::*)(void),&OtherStruct::foo>
typedef typename MagicTemplate<&OtherStruct::foo>::Type struct_defC;
你可以等C++17。
或者你平底船;拿一个类型T
,T
应该与std::integral_constant
兼容。 这使罐子在路上更进一步;但是,制作特定类型integral_constant
的快捷方式比制作某些定制模板实例的快捷方式更干净。
例如,我可以123_k
设置为std::integral_constant<int,123>
类型。 做MyStruct<decltype(123_k)>
.
作为奖励,您的MyStruct
现在与template<class...>
匹配,这使得某些元编程更容易。
相关文章:
- Keil armcc 生成类型信息名称,即使使用 --no_rtti_data
- 错误:未定义对"静脉类型信息::电池访问"的引用
- 如何在编译过程中输出C 类型信息
- 如何在C 中检查确切的类型信息(具有CV-REF-POIRT特征)
- 共享库中非模板基的模板子类导致未定义的符号类型信息'class'链接错误
- 为什么C++不使用集中存储类型信息以实现高效的 RTTI
- 给定DDS主题名称,可以在运行时确定主题类型信息
- 使用友元函数从多态类中检索类型信息
- 如何在函数模板中隐式推导数组中元素的类型信息
- 使用标准类型的动态类型信息实例化标识符
- 超载功能没有上下文类型信息
- 对"vtable for "命名空间继承的未定义引用 对"类型信息"的未定义引用
- 多映射插入键类型信息 std::make_pair vs std::p air 构造函数
- Visual Studio-使用PDB进行调试,但缺少类型信息
- 如何在不同的上下文(例如线程)中保留原始异常类型信息
- C++正在传输运行时条件类型信息
- C++虚拟表上的虚拟定义器和类的类型信息
- C++测试宏定义的类型信息值
- 编译器不可知的运行时类型信息
- C++类型信息