This question follows : Force a specific overload when template template
Consider the following code :
#include <iostream>
#include <vector>
#include <array>
#include <type_traits>
// Version A
template<typename T>
void f(const T& x)
{
std::cout<<"Version A"<<std::endl;
}
// Version B
template<typename... T1, template<typename...> class T>
void f(const T<T1...>& x)
{
std::cout<<"Version B"<<std::endl;
}
// Version C
template<typename T1, typename TN, template<typename, TN...> class T, TN... N>
void f(const T<T1, N...>& x)
{
std::cout<<"Version C"<<std::endl;
}
// Main
int main(int argc, char* argv[])
{
f(double());
f(std::vector<double>());
f(std::array<double, 3>()); // <- How to force the use of Version C ?
return 0;
}
By default, it will produce (with GCC 4.7.1) :
Version A
Version B
Version A
How to force the use of Version C when the passed type is a template with the good shape (I can add new versions of f, I can add std::enable_if or other C++11 type traits syntax, but if possible I would like to avoid adding an helper class) ?
Note : The trick should work for every integral type TN…
You can’t deduce the type of a non-type template argument in template argument deduction. This is explicitly stated in the standard:
This works: