从模板类获取"sub-type"
Getting a "sub-type" from a template class
假设我有这个类
template<
typename T1, /*this is probably an integral type*/
T1 Default /*this is a typical value of that integral type*/
> class Foo {};
以及给定T1
和Default
的实例化,例如foo
.
我可以使用decltype(foo)
来获取完整的类型。
是否有一些语法可以用来获取值Default
?
只需在课堂上使用typedef
即可。
template<
typename T1,
typename T2
> class Foo
{
public:
typedef T1 type1;
typedef T2 type2;
};
要获得默认值,您实际上可以使用相同的语法。
template<
typename T1,
T1 Default
> class Foo
{
public:
typedef T1 type1;
static constexpr const T1 default_value = Default;
};
你也可以写一个元函数来拉出它:
template <typename T> struct my_trait;
template <typename T, T Value>
struct my_trait<Foo<T, Value>>
{
using T1 = T;
static const T1 Default = Value;
};
因此使用:
Foo<int, 42> myfoo;
std::cout << "Default is " << my_trait<decltype(myfoo)>::Default;
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- Cython:如何从C级类型获取'actual Python type'(类型代码/dtype)
- 如何获取多类型向量中的值(不添加 <type>)?
- 如何使用Boost.Process从'type'(窗口)获取跟踪?
- 获取lib clang cursor/type的实际拼写
- 获取"Use of undefined type"和"Must have class/struct/union"错误
- 如何在C++中获取任意 (STL) 输出迭代器的"assignable type"
- 如何获取有关结构/类内部"current type"的信息?
- 从模板类获取"sub-type"
- 使用 xerces 读取 XML:获取类型,其中 <nodeName type= "typeName" >
- 从Range Type中获取迭代器类型
- 获取"terminating with uncaught exception of type std::length_error: vector"错误C++
- 获取"does not name a type error",但我正在命名一个类型
- 使用boost::regex获取sub-match_results