简体   繁体   中英

Retrieve the type of an unnamed struct for using it in a member function

How to get the type of an unnamed struct for using it in a member function?

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;
}

You can make the operator< a template, and constrain the type with std::enable_if . Eg

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM