类层次结构中的运算符重载

Operator overloads within a class heirarchy

本文关键字:运算符 重载 层次结构      更新时间:2023-10-16

如果之前有人问过并回答过,我很抱歉,但我现在一直在寻找一个可以理解的解决方案几个小时,没有快乐。

我正在尝试实现一个简单的类层次结构,并在基类中定义运算符重载(因为这些在各种派生类之间不会有所不同(。 但是,由于其中大多数都需要返回我们在上下文中的任何派生类的新对象,因此我假设它们需要声明为模板化方法,但是,当我尝试编译这样做时,链接器给了我一个"未解析的外部符号"错误......

举个例子:

/// This is in the class header file
class Base
{
// Declare all the base storage and methods ...
public:
template<class T>
friend T operator+(const T& lhs, const T& rhs);
// .... other stuff in here.
}

/// Then in the implementation file
T operator+ (const T& lhs, const T& rhs)
{
return T(lhs->m_1 + rhs->m_1, lhs->m_2);
}

我希望这会导致我能够声明派生对象,这样:

class Derived : public Base
{
// ... Add the derived class functionality
// -- Question:  Do I need to do anything in here with operator+ ???
}
Derived A();
Derived B();
Derived C = A + B;

是所需的结果,但除了在每个单独的派生类中定义运算符外,我看不到可以在C++中实现它的方法,因为模板方法会导致链接器错误。

我是否错过了一些显而易见和基本的东西,或者根本没有简单的方法来做到这一点C++?

您的注释指示模板的"在实现文件中",这可能是导致问题的原因。函数模板声明(例如T operator+(const T&, const&);声明必须链接的符号 - 但它需要在某处实例该模板。

简单地在源文件中定义template函数实际上并不会实例化代码 - 它需要显式希望链接的每种类型的具体实例化,或者使函数模板定义从打算调用它的位置可见(有关更多详细信息,请参阅此答案(。

在许多情况下,最好在头文件中定义函数模板,以便模板的任何调用方都能够实例化函数,而无需链接到其他位置的现有实例化。


可是。。。

您可能需要重新考虑当前的方法。operator+模板不限制可以考虑的T类型,这将导致operator+(const T&, const T&)成为任何尚未具有operator+T类型的可行重载,前提是声明在重载解析期间可见。这可能会导致其他奇怪的编译器/链接器错误。

有几种方法可以解决约束类型;可能最简单的方法是使用 SFINAE 通过检查T是否派生自Base来约束它。

例如:

/// This is in the class header file
class Base
{
// Declare all the base storage and methods ...
public:
template<class T, class>
friend T operator+(const T& lhs, const T& rhs);
// .... other stuff in here.
}

// Note: In the same header!
// This only enables + if 'T' derives from 'Base'
template <typename T, typename = std::enable_if_t<std::is_base_of<Base,T>::value>>
T operator+(const T& lhs, const T& rhs)
{
return T(lhs->m_1 + rhs->m_1, lhs->m_2);
}