继承成员功能的模板专业化
Template specialization of inherited member function
我们想专业化基类的成员功能。但是,它没有编译。有人知道有任何编译的选择吗?
这是一个示例
struct Base
{
template<typename T>
void Foo()
{
throw "Foo() is not defined for this type";
}
};
struct Derived : public Base
{
template<>
void Foo<int>() { cout << "Foo<int>()" << endl; } // compile error (cannot specialize members from a base class)
template<>
void Foo<double>() { cout << "Foo<double>()" << endl; } // compile error (cannot specialize members from a base class)
};
最终,我们使用过载解决了它。
这是基类的样子
struct Base
{
template<typename T>
class OfType {}
template<typename T>
void Foo(OfType<T>) { static_assert(false, "Foo is not implemented for this type. Please look in the compiler error for more details."); }
};
struct Derived : public Base
{
using Base::Foo;
void Foo(OfType<int>) { // here comes logic for ints }
void Foo(OfType<double>) { // here comes logic for doubles }
};
这是使用Foo()
template<typename S>
class ClassThatUsesFoo
{
private: S s;
template<typename T>
void Bar(T item)
{
s.Foo(Base::OfType<T>()); // this is the code that uses Foo
DoSomeStuffWithItem(item);
}
};
void main()
{
ClassThatUsesFoo<Derived> baz;
baz.Bar(12); // this will internally use Foo for ints
baz.Bar(12.0); // this will use Foo for doubles
baz.Bar("hello world"); // this will give a verbose compile error
}
这将编译,除了呼叫Foo<char>()
:
#include <iostream>
#include <string>
using namespace std;
struct Base
{
template<typename T>
void Foo()
{
throw "Foo() is not defined for this type";
}
};
struct Derived : public Base
{
template<typename T> void Foo();
};
template<>
void Derived::Foo<int>() { cout << "Foo<int>()" << endl; }
template<>
void Derived::Foo<double>() { cout << "Foo<double>()" << endl; }
int main()
{
Derived derived;
// this is client code
derived.Foo<int>();
derived.Foo<double>();
derived.Foo<char>(); // this throws
}
如果您希望呼叫Foo<char>()
或任何不专门专门专门专门说明的类型,则可以使用。如果您需要适用于所有类型的非专业实现,则需要添加Foo()
的非专业实现:
template<typename T>
void Derived::Foo() { cout << "generic" << endl; }
响应与Alex的讨论(请参阅John Dibling的回答评论),这是我的意思(SSCCE):
#include <iostream>
using namespace std;
struct Base
{
template<typename T>
void Foo()
{
//static_assert(false, "Foo() is not defined for this type");
throw "Foo() is not defined for this type";
}
};
// you can add as many specializations in Base as you like
template <>
void Base::Foo<char>() { cout << "Base::Foo<char>()" << endl; }
struct Derived : public Base
{
// just provide a default implementation of Derived::Foo
// that redirects the call to the hidden Base::Foo
template < typename T >
void Foo()
{ Base::Foo<T>(); }
};
// the specializations for Derived
template<>
void Derived::Foo<int>() { cout << "Foo<int>()" << endl; }
template<>
void Derived::Foo<double>() { cout << "Foo<double>()" << endl; }
struct Derived_wo_specialization : public Base
{
/* nothing */
};
int main()
{
Derived d;
d.Foo<char>();
d.Foo<double>();
Derived_wo_specialization dws;
dws.Foo<char>();
dws.Foo<double>();
}
相关文章:
- 类模板的成员功能的定义在单独的TU中完全专业化
- 模板类专业化具有成员变量和方法的默认值
- C++关于非专用模板的成员模板专业化的标准段落
- 我们可以专业化类模板的枚举(类型)成员吗?
- 对完整模板专业类成员功能的未定义引用,但不是部分专业化
- 为什么不允许对成员函数的模板专业化
- 成员函数模板参数的部分专业化
- 成员的部分专业化
- 如何定义模板类专业化的静态constexpr数组成员
- 通过专业化将班级成员排除在外
- 围绕缺乏局部成员专业化,具有特定的设计限制
- 功能指针数组(包括成员功能)投掷模板专业化错误
- 特定成员函数的部分专业化
- 是否可以使用std :: enable_if选择成员模板专业化
- 继承成员功能的模板专业化
- 实例化错误后成员函数模板的专业化,以及成员函数的顺序
- 成员功能模板专业化是否可以具有与主模板不同的访问级别
- 成员功能的部分专业化
- 我可以只为特定的专业化定义静态constexpr数据成员吗
- 模板专业化-成员函数