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?
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.
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.
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.
8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With