Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does this template definition mean in c++?

Tags:

c++

c++11

I am reading some c++ code, and the code list below makes me really confused.
I can guess it tries to define a specialized template, which tries to trait the input args types. But I got a few question:
the first template looks like both deriving and template specialization, if it is deriving, how can a struct derived from itself? if it is a template specialization, where is the template defination?

template<typename F>
struct function_traits : public function_traits<decltype(&F::operator())>
{};

template<typename R, typename C, typename ... Args>
struct function_traits<R(C::*)(Args...) const> {
    template<size_t i>
    struct arg
    {
        using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
    };
};
like image 367
D.pz Avatar asked Sep 11 '18 11:09

D.pz


1 Answers

The first declaration is just a definition of the primary template for struct function_traits. Note that there's no problem with a particular specialisation of a template being derived from a different specialisation of that template (as long as there are no loops). Remember that each specialisation of a class template is a distinct, unrelated type.

The second declaration simply introduces a partial specialisation for function types, whose instantiations will likely get used as the base-class for the primary template (since the primary template derives from a specialisation whose template argument is a member function type).

like image 187
Angew is no longer proud of SO Avatar answered Sep 28 '22 00:09

Angew is no longer proud of SO