【发布时间】:2015-01-28 16:28:58
【问题描述】:
以下代码fails to compile:
#include <iostream>
template<typename F, typename ...Args>
static auto wrap(F func, Args&&... args)
{
return func(std::forward<Args>(args)...);
}
void f1(int, char, double)
{
std::cout << "do nada1\n";
}
void f2(int, char='a', double=0.)
{
std::cout << "do nada2\n";
}
int main()
{
wrap(f1, 1, 'a', 2.);
wrap(f2, 1, 'a');
}
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In instantiation of 'auto wrap(F, Args&& ...) [with F = void(*)(int, char, double); Args = {int, char}]':
main.cpp:22:20: required from here
main.cpp:6:44: error: too few arguments to function
return func(std::forward<Args>(args)...);
似乎遵循了关于“参数包最后”的规则(至少在声明中)并且在扩展之后应该形成正确的函数调用:f2 可以用 1、2 或 3 个参数调用所以too few arguments 是一个错误似乎“苛刻”。它看起来也不像 deduction problem(这是我的猜测 - 但由于错误消息而变得不稳定)
这是缺少的功能还是从标准的角度来看存在违规行为?
【问题讨论】:
-
它与 clang 3.5 引发类似的错误
-
VS2013 给出这个:main.cpp(7): error C3551: expected a trailing return type
标签: c++ c++11 parameter-passing variadic-templates c++14