Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template Template Parameters in the C++ Standard Library?

Tags:

c++

c++11

c++14

Which templates (if any) in the C++ standard library have one or more template template parameters?

If there are many, then a couple of examples is fine.

If C++ version matters, then the latest draft of C++14/C++1y is preffered.

like image 844
Andrew Tomazos Avatar asked Aug 28 '14 21:08

Andrew Tomazos


1 Answers

I'm not aware of any templates in the C++ standard library that are specified to take a template template parameter, but there is at least one standard template in C++11 that has a partial specialization with a template template parameter: std::pointer_traits. std::pointer_traits<Ptr>::element_type is specified to be:

Ptr::element_type if such a type exists; otherwise, T if Ptr is a class template instantiation of the form SomePointer<T, Args>, where Args is zero or more type arguments; otherwise, the specialization is ill-formed.

In order to implement this you need a template template parameter for SomePointer, because it can be an arbitrary class template (as long as it only has type template parameters, to be precise). Here is the libstdc++ helper class partial specialization that does this, for instance:

  template<template<typename, typename...> class _SomePtr, typename _Tp,
            typename... _Args>
    struct __ptrtr_elt_type<_SomePtr<_Tp, _Args...>, false>
    {
      typedef _Tp __type;
    };
like image 146
Brian Bi Avatar answered Nov 15 '22 22:11

Brian Bi