【发布时间】:2017-07-05 07:10:18
【问题描述】:
根据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++ 允许在可变参数后面加上普通参数吗?
【问题讨论】:
-
我的编译器是clang 3.8。 gcc 6.2 也失败了。
-
请注意,对于
lock_guard示例,它是一个类构造函数,因此所有模板参数都是预先知道的,而不是试图从函数调用参数中推断出来。 -
是 C++17,不是 C++11。
-
确切地说:可变参数是从 C 继承的一个特性,它们必须是最后一个。您在询问 C++11 中引入的参数包。
-
@tad 你能否为 C 特性称为“可变参数”的说法提供证明或参考?
标签: c++ c++11 templates standards variadic-templates