Does C ++ provide normal parameters after variational pattern parameters?

According to cppreference , the following code is legal:

lock_guard( MutexTypes&... m, std::adopt_lock_t t ); 

However, the following code cannot be compiled with clang 3.8 (-std = C ++ 1z):

 template<typename... Args> void f(Args&&..., bool) {} int main() { f(1, 2, 3, true); // error! see below for details. } 
 1>main.cpp(59,2): error : no matching function for call to 'f' 1> f(1, 2, 3, true); 1> ^ 1> main.cpp(54,6) : note: candidate function not viable: requires 1 argument, but 4 were provided 1> void f(Args&&..., bool) 1> ^ 1> 1 error generated. 

Does C ++ provide normal parameters after variational parameters?

+7
c ++ standards c ++ 11 templates variadic-templates
source share
1 answer

Declaring a function in your code is acceptable, but deduction does not work properly for such function templates. Note that the following code is well-formed and creates the specialization void f(int, int, int, bool) :

 template<typename... Args> void f(Args&&..., bool) {} int main() { f<int, int, int>(1, 2, 3, true); } 

Note that in C ++ 17, MutexTypes... are template parameters for the class itself:

 template <class... MutexTypes> class lock_guard; 

therefore, they are known and do not need to be deduced. Please note that the constructor with adopt_lock_t cannot be used to output the C ++ 17 class template argument, since the adopt_lock_t argument occurs after the parameter package. If the committee were predictable in C ++ 11, they would put adopt_lock_t at the beginning, not the end, but, alas, it's too late.

+9
source share

All Articles