C 是否允许在变异模板参数之后进行普通参数

Does C++ allow normal parameters after variadic template parameters?

本文关键字:参数 之后 是否 变异      更新时间:2023-10-16

根据cppreference,以下代码是合法的:

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

但是,以下代码无法使用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.

C 是否允许在变化参数后允许普通参数?

您代码中的函数声明是有效的,但是 dectuction 对此类函数模板不正常。请注意,以下代码构成良好,并实例化专业void f(int, int, int, bool)

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

请注意,在C 17中,MutexTypes...是类本身的模板参数:

template <class... MutexTypes> class lock_guard;

因此,它们是已知的,不需要推导。请注意,带有adopt_lock_t的构造函数不能用于C 17类模板参数扣除,因为adopt_lock_t参数发生在参数包之后。如果委员会在C 11中有先见之明,他们将在开始时就将adopt_lock_t的论点提出来,而不是在最后,但是现在为时已晚。