【发布时间】:2013-03-15 14:02:44
【问题描述】:
我在程序中使用可变参数模板,但出现了意外错误。我隔离了错误并震惊了它:
#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
我不明白为什么编译器会抛出这个错误。这可能是一个错误吗?
【问题讨论】:
-
你给它一个重载的函数,编译器应该如何选择你想要的 std::isspace 的重载?
-
为什么你马上就认为这是一个编译器错误?太有自信了……
-
我不认为这是编译器错误。在标题中我使用了疑问句,在帖子中我想知道这是否是一个可能的错误。
标签: c++ templates c++11 variadic-templates