[英]meaning of c++ struct syntax with only typedef
When I examine some code snip from some libraries, I saw some code like this: 当我检查某些库中的某些代码片段时,我看到了一些类似以下的代码:
template<typename _Function, typename _ReturnType>
struct _TaskOfType_ContinuationTypeTraits
{
typedef task<typename _TaskTypeTraits<typename _FunctionTypeTraits<_Function, _ReturnType>::_FuncRetType>::_TaskRetType> _TaskOfType;
};
Can someone provide some explanation of the code? 有人可以提供一些代码解释吗? What is it trying to do and what is the advantage to use a struct with only typedef statement within the body?
它试图做什么?在主体中使用仅带typedef语句的结构有什么好处?
In C++ parlance, the _TaskOfType_ContinuationTypeTraits
is a metafunction. 用C ++的话来说,
_TaskOfType_ContinuationTypeTraits
是一个元函数。 It does type computations at compile-time. 它在编译时进行类型计算。
A metafunction is in a way similar to a run-time function. 元功能在某种程度上类似于运行时功能。 The key difference is that the input arguments to a metafunction are type(s), and returns are also type(s).
关键区别在于元函数的输入参数是类型,而返回值也是类型。
Eg. 例如。 The following metafunction takes a type, and returns a pointer of the type you supply to it.
以下元函数接受一个类型,并返回您为其提供的类型的指针。
template <typename T>
struct add_pointer
{
typedef T* type;
}
Now, if you did add_pointer<int>::type
, it returns int*
. 现在,如果您执行了
add_pointer<int>::type
,它将返回int*
。 You see, you gave it a type ( int
in this case) and the compiler computed a new type ( int*
in this case) and gave it back to you when you invoked the ::type
of the metafunction. 可以看到,给了它一个类型(在这种情况下为
int
),编译器计算了一个新类型(在这种情况下为int*
),并在调用元函数的::: ::type
时将其还给了您。 When you do ::type
on a metafunction, that is when the template is instantiated. 在元函数上执行
::type
时,即实例化模板时。 This is the run-time equivalent of calling a function. 这相当于调用一个函数的运行时。 Also note that all of this happened at compile-time!
还要注意,所有这些都是在编译时发生的!
Now, going back to your _TaskOfType_ContinuationTypeTraits
. 现在,返回到
_TaskOfType_ContinuationTypeTraits
。 This is just like my add_pointer
. 就像我的
add_pointer
。 In add_pointer
, I just had one template argument, you have two. 在
add_pointer
,我只有一个模板参数,而您有两个。 I just added a pointer to the type that was supplied, you have something much more complicated. 我只是添加了一个指向所提供类型的指针,所以您要复杂得多。 But, at it's essence, it is only a type computation.
但是,从本质上讲,它只是类型计算。 My
add_pointer
returns when I call ::type
on it, yours does when you call ::_TaskOfType
. 当我调用
::type
时,我的add_pointer
返回,而当调用::_TaskOfType
时,您的::_TaskOfType
。
This kind of syntax is used to create "templated typedefs". 这种语法用于创建“模板化typedef”。 In C++11 and above, type aliases / alias templates should be used instead.
在C ++ 11及更高版本中,应改用类型别名/别名模板 。
The purpose of the code snippet you posted is to create a type alias that depends on _Function
and _ReturnType
. 您发布的代码段的目的是创建依赖于
_Function
和_ReturnType
的类型别名。
It can be accessed like this: 可以这样访问:
typename _TaskOfType_ContinuationTypeTraits<F, R>::_TaskOfType
If you have access to C++11, this is a cleaner, better and more straightforward solution: 如果您可以使用C ++ 11,则这是一种更干净,更好,更直接的解决方案:
template<typename _Function, typename _ReturnType>
using _TaskOfType =
task<typename _TaskTypeTraits<
typename _FunctionTypeTraits<_Function, _ReturnType>::_FuncRetType>::_TaskRetType>
Which can be used like this: 可以这样使用:
_TaskOfType<F, R>
More info: "Difference between typedef and C++11 type alias" 更多信息: “ typedef和C ++ 11类型别名之间的区别”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.