代码之家  ›  专栏  ›  技术社区  ›  3CxEZiVlQ

检索未命名结构的类型,以便在成员函数中使用它

  •  0
  • 3CxEZiVlQ  · 技术社区  · 4 年前

    struct {
      using P = int;  // What should be instead of int?
    
      bool operator<(const P& r) {
        return false;
      }
    } obj1, obj2;
    
    int main() {
      obj1 < obj2;
      return 0;
    }
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   songyuanyao    4 年前

    你可以 operator< 模板,并使用 std::enable_if . 例如。

    template <typename P>
    auto operator<(const P& r) -> std::enable_if_t<std::is_same_v<P, std::remove_reference_t<decltype(*this)>>, bool> {
      return false;
    }