我在程序中使用了可變參數模板,並且出現了一個意外的錯誤。我孤立的錯誤,我震驚了它:與可變參數模板的罕見錯誤?
#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;
}
如果刪除行「#包括的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的哪個重載之間選擇? – PlasmaHH 2013-03-15 14:11:44
爲什麼你馬上認爲它是一個編譯器錯誤?這是非常有信心... – 2013-03-15 14:13:24
我不假設它是一個編譯器錯誤。在標題中我使用了疑問句,並且在帖子中,我想知道這是否是一個可能的錯誤。 – xgbuils 2013-03-15 14:35:36