user2984297 user2984297 - 3 months ago 8
C++ Question

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?

Answer

This kind of syntax is used to create "templated typedefs". In C++11 and above, type aliases / alias templates should be used instead.

The purpose of the code snippet you posted is to create a type alias that depends on _Function and _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:

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"