【问题标题】:How to define non-member operator overload for class template?如何为类模板定义非成员运算符重载?
【发布时间】:2017-08-03 12:41:58
【问题描述】:

我有一个类模板,它的构造函数采用 std::chrono::duration,因为我希望能够使用 chrono_literals 来构造它。现在,我正在尝试定义一个非成员运算符重载,但我无法让它与持续时间构造函数一起使用:

#include <chrono>
#include <iostream>

using namespace std;

template <int n> struct MyClass {
  MyClass() = default;

  template <typename REP, typename PERIOD>
  constexpr MyClass(const std::chrono::duration<REP, PERIOD> &d) noexcept
      : num(d.count()) {}

  int num = n;
};

template <int n> bool operator==(MyClass<n> lhs, MyClass<n> rhs) {
  return lhs.num == rhs.num;
}

int main(int argc, char *argv[]) {
  using namespace std::literals::chrono_literals;

  MyClass<0> m1(10ns);

  if (m1 == 10ns)
    cout << "Yay!" << endl;
  return 0;
}

gcc 因拒绝我的重载而给出此错误:

main.cpp:34:12: error: no match for ‘operator==’ (operand types are ‘MyClass<0>’ and ‘std::chrono::nanoseconds {aka std::chrono::duration<long int, std::ratio<1l, 1000000000l> >}’)
     if (m1 == 10ns)
         ~~~^~~~~~~
main.cpp:23:6: note: candidate: template<int n> bool operator==(MyClass<n>, MyClass<n>)
 bool operator == (MyClass<n> lhs, MyClass<n> rhs)
      ^~~~~~~~
main.cpp:23:6: note:   template argument deduction/substitution failed:
main.cpp:34:15: note:   ‘std::chrono::duration<long int, std::ratio<1l, 1000000000l> >’ is not derived from ‘MyClass<n>’
     if (m1 == 10ns)
               ^~~~

有什么办法可以做到吗?

【问题讨论】:

  • 这需要模板推导、用户定义的转换和重载解析的特定组合。而且它们不会按照此代码要求的顺序发生。

标签: c++ c++11 templates operator-overloading c++14


【解决方案1】:

更简单的方法是将函数放在类中:

template <int n> struct MyClass {
  MyClass() = default;

  template <typename REP, typename PERIOD>
  constexpr MyClass(const std::chrono::duration<REP, PERIOD> &d) noexcept
      : num(d.count()) {}

    friend bool operator==(MyClass lhs, MyClass rhs) { return lhs.num == rhs.num; }


  int num = n;
};

Demo

【讨论】:

  • 这是一个很好的解决方案,谢谢!如果我不希望函数:1. 被声明为朋友,2. 在类定义中定义怎么办?我想我在问,这是“更简单”的方式还是唯一方式?
  • @dcmm88 根据定义,友元函数是非成员函数,就像你在类外声明它一样。作为奖励,您可以使用 your 类做您想做的事,包括访问私有成员。在这种情况下使用朋友没有任何不利之处。但是,您可以在类之外定义朋友函数,但您仍然必须在内部声明它。
  • @GuillaumeRacicot 我认为这实际上行不通(在内部声明并在外部定义)。我将如何使用模板在外部定义它?然后我回到原来的问题,不是吗?
  • @dcmm88 确实,在外面定义朋友并不像在类中简单地编写函数那么容易。正如 Barry 所说,还有其他解决方案,但最简单的一个是定义朋友。
【解决方案2】:

这不起作用:

if (m1 == 10ns)

因为当我们在operator== 之间查找MyClass&lt;0&gt;std::chrono::duration&lt;???, std::nano&gt; 时,我们找到的唯一运算符是:

template <int n>
bool operator==(MyClass<n> lhs, MyClass<n> rhs);

这不匹配 - 10ns 不是任何 nMyClass&lt;n&gt;,因此模板推导失败。要编写非成员相等运算符,您需要匹配 any 持续时间:

template <int n, class R, class P> bool operator==(MyClass<n>, duration<R,P> );

双向:

template <int n, class R, class P> bool operator==(duration<R,P>, MyClass<n> );

除了您已经拥有的运算符。这会奏效,有时甚至是必要的。

按照Jarod42 的建议,一种更简单的方法是将您的operator== 声明为非会员朋友。这样做的原因是你的非成员函数是一个函数模板,而朋友是 not。所以在m1 == 10ns 上查找函数:

bool operator==(MyClass<0>, MyClass<0>);

10ns 可转换为MyClass&lt;0&gt;,这在此上下文中是允许的,所以这是可行的。这种转换非常便宜,所以不用担心。你只需要编写一个函数。

【讨论】:

  • 或将两个参数之一包装在非推导上下文中。
猜你喜欢
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2019-03-30
  • 2019-01-21
相关资源
最近更新 更多