我在程序中使用可变参数模板,但出现了意外错误。我隔离了错误,我对此感到震惊:
#include<cctype>
#include<iostream> // try to delete this line
class A
{
public:
void constructor()
{ }
template<typename... Args>
void constructor( int (*f)(int), Args... args )
{
// process( f )
constructor( args... );
}
template<typename... Args>
A( Args... args )
{
constructor( args... );
}
};
int main()
{
A a;
a.constructor( std::isspace ); // ok
A b( std::isspace ); // error
return 0;
}
如果删除“#include iostream”行,源代码编译正常。但是,如果您输入这一行,编译器会抛出错误:
prov.cpp: In function ‘int main()’:
prov.cpp:32:22: error: no matching function for call to ‘A::A(<unresolved overloaded function type>)’
prov.cpp:32:22: note: candidates are:
prov.cpp:18:7: note: A::A(Args ...) [with Args = {}]
prov.cpp:18:7: note: candidate expects 0 arguments, 1 provided
prov.cpp:4:7: note: constexpr A::A(const A&)
prov.cpp:4:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘const A&’
prov.cpp:4:7: note: constexpr A::A(A&&)
prov.cpp:4:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘A&&’
我正在使用这个 g++ 版本:g++ (Ubuntu/Linaro 4.7.2-11precise2) 4.7.2,我正在使用以下标志进行编译:g++ -Wall -pedantic -std=c++11 prov.cpp -o prov
我不明白为什么编译器会抛出这个错误。这是一个可能的错误吗?