Sur*_*rya 11 c++ inheritance templates
我有一个带功能模板的基类.
我派生自基类,并尝试在派生类中对函数模板进行专门化
我做了这样的事.
class Base
{
..
template <typename T>
fun (T arg) { ... }
};
class Derived : public Base
{
...
} ;
template <>
Derived::fun(int arg);
Run Code Online (Sandbox Code Playgroud)
在.cpp文件中,我提供了模板特化的实现.
这适用于MSVC 8.0和g ++ - 4.4.2抱怨Derived类中缺少函数声明乐趣.
我不知道哪个编译器的行为正确.非常感谢任何帮助.
谢谢,苏里亚
您需要在Derived中声明该函数,以便能够重载它:
class Derived : public Base
{
template <typename T>
void fun (T arg)
{
Base::fun<T>(arg);
}
} ;
template <>
void Derived::fun<int>(int arg)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
请注意,您可能需要内联特化或将其移动到实现文件,在这种情况下,您必须将头文件中的特化原型设置为:
template <>
void Derived::fun<int>(int arg);
Run Code Online (Sandbox Code Playgroud)
否则编译器将使用通用版本的"fun"在调用代码时生成代码,而不是链接到特化.
归档时间: |
|
查看次数: |
6183 次 |
最近记录: |