C++ 使用函数本地类拒绝模板实例化
可能重复:C++ 使用函数本地类拒绝模板实例化,c++,templates,C++,Templates,可能重复: g++4.4拒绝编译对使用函数本地类作为模板参数的模板函数的调用。像这样: // Given this: template <typename C> int f(const C& c) { return c.g(); } // This compiles fine: struct C1 { int g() const { return 42; } }; int h1() { return f(C1()); } // But this d
g++4.4拒绝编译对使用函数本地类作为模板参数的模板函数的调用。像这样:
// Given this:
template <typename C>
int f(const C& c) {
return c.g();
}
// This compiles fine:
struct C1 {
int g() const { return 42; }
};
int h1() {
return f(C1());
}
// But this doesn't:
int h2() {
struct C2 {
int g() const { return 42; }
};
return f(C2()); // error: no matching function for call to "f(h2()::C2)"
}
// Nor does this:
int h3() {
struct C3 {
int g() const { return 42; }
};
return f<C3>(C3()); // same error
}
//鉴于此:
模板
内部f(常数C&C){
返回c.g();
}
//这很好:
结构C1{
int g()常量{return 42;}
};
int h1(){
返回f(C1());
}
//但这并不是:
int h2(){
结构C2{
int g()常量{return 42;}
};
返回f(C2());//错误:对“f(h2()::C2)”的调用没有匹配的函数
}
//这也不是:
int h3(){
结构C3{
int g()常量{return 42;}
};
返回f(C3());//相同的错误
}
有什么好处?我该怎么做?(在从中删减的实际程序中,“h”是一个成员函数,“C”必须是一个嵌套类,以便它隐式地是“h”是其成员的类的朋友。)本地类可能不是模板参数
模板参数必须具有外部链接。
现在,您可以将ci设置为一个适当的嵌套类(在
h
的类中,而不是在h
的类中)。请参阅章节?或者是进一步解释的链接?@Zac正在寻找链接,已更新information@Zac可能会为您解决问题:谢谢您的解决方案。这很不幸,因为“C”只在那个地方使用,但至少我不必公开不应该公开的东西。