繁体   English   中英

在可变参数模板参数之后,C ++是否允许正常参数?

[英]Does C++ allow normal parameters after variadic template parameters?

根据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 ++是否允许正常参数?

代码中的函数声明是有效的,但是对于此类函数模板, 演绎不起作用。 请注意,以下代码格式正确,并实例化了特殊化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论点放在开头而不是最后,但唉,现在已经太晚了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM