4
我有一個模板類,在其中定義引用該模板類的自由函數。這些免費功能也在不同的參數上模板化。如何引用C++中的雙模板自由函數
從課外,我可以撥打免費的功能。但是,我找不到一個免費函數調用另一個的正確語法。
簡單的例子:
template<typename T> class Foo {
template<typename S>
friend S f(const Foo &) { return S(); }
template<typename S>
friend S g(const Foo &s) {
return f(s); // See below, when instantiated, yields 'no matching function for call to f(const Foo &)'
}
};
float test1() {
Foo<int> o;
return f<float>(o); // Compiles
}
float test2() {
Foo<int> o;
return g<float>(o); // Fails to compile as line above errors
}
(C.F. this link太)
似乎被調用點到g內F(S)(),最外面的模板已丟失。我如何在f調用中重新指定T?我檢查了GCC4.7,4.8,clang 3.2都有同樣的錯誤。
你能證明聲明是什麼樣子?模板化的朋友接受模板化的參數不具有簡單的語法。 –
謝謝 - 這很完美。我的最終解決方案是: template class Foo { }; template S f(const Foo &){return S(); } template S g(const Foo &o){return f
(o);} } float test2(){ Foo o; return g (o); } –
@BenVoigt,我已經這麼做了。 –