PiotrNycz
PiotrNycz

Reputation: 24402

Is any C++20 feature test for defaulted equal operator?

I wanted to use defaulted equal operator bool operator ==(...) = default; if only compiler supports it, otherwise I could still use existing implementation.

But I cannot find an appropriate feature test in https://en.cppreference.com/w/cpp/feature_test

Point me the correct feature test or help to fix this code:


struct Some {
  constexpr bool operator == (const Some& rhs) const noexcept 
#if __cpp_what?
   = default;
#else
   {
      ...
   }
#endif
};

Upvotes: 4

Views: 344

Answers (2)

Barry
Barry

Reputation: 302852

The document you're looking for is SD-FeatureTest. It ties in all the proposals and all the history of all the macro values.

Defaulted comparison is slightly unique in this sense, in that while it is a language feature, there is also a strong library component to it: in order to use <=>, you need to #include <compare> and use the comparison types defined in that header. As such, there are two macros here:

  • __cpp_impl_three_way_comparison
  • __cpp_lib_three_way_comparison

The language macro is really intended for the standard library to provide the library facility only when the compiler can support it.

The library macro is the one intended for the user (i.e., you) to use.

Upvotes: 2

dfrib
dfrib

Reputation: 73186

As default comparison is tightly connected to

  • P1186R3: When do you actually use <=>?

you may be able make use of the __cpp_impl_three_way_comparison feature test also for the subset case of default comparison. See also P1185R2 (<=> != ==).

Upvotes: 4

Related Questions