Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is default template template parameter value deduced context?

I was actually surprised that both gcc and clang accept this code:

#include <iostream>
#include <vector>
#include <type_traits>

template <class T, template <class, class = T> class TT, class Y>
T foo(TT<Y>) {
}

int main() {
    static_assert(std::is_same<decltype(foo(std::vector<int>{})), std::allocator<int>>::value);
}

Are gcc and clang right that the values of the default template template parameters are deduced context or is it compilers extension?

like image 536
W.F. Avatar asked Dec 07 '16 20:12

W.F.


People also ask

Can template parameters have default values?

Just like in case of the function arguments, template parameters can have their default values. All template parameters with a default value have to be declared at the end of the template parameter list.

Can a template be a template parameter?

Templates can be template parameters. In this case, they are called template parameters. The container adaptors std::stack, std::queue, and std::priority_queue use per default a std::deque to hold their arguments, but you can use a different container. Their usage is straightforward.

What is a template template parameter in C++?

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

Why do we use template template parameter?

8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.


1 Answers

When you write

template <class T, template <class, class = T> class TT, class Y>
T foo(TT<Y>);

it is equivalent to

template <class T, template <class, class = T> class TT, class Y>
T foo(TT<Y, T>);

So T can be deduced.

like image 107
Jarod42 Avatar answered Sep 21 '22 08:09

Jarod42