【发布时间】:2023-08-15 18:17:01
【问题描述】:
我想有条件地在我的代码中启用operator <=> 重载,具体取决于是否支持给定当前版本的编译器及其命令行选项。例如,我希望将以下代码编译为 C++14、17 和 20(这实际上是我之前提出的问题的 this solution 的续集):
#define SPACESHIP_OPERATOR_IS_SUPPORTED 1 // <--- i want this to be automatic
#if SPACESHIP_OPERATOR_IS_SUPPORTED
#include <compare>
#endif
template <int N> struct thing {
// assume an implicit conversion to a "math-able" type exists:
operator int () const { return 0; }
// define a set of comparison operators for same N on rhs:
bool operator == (const thing<N> &) const { return true; }
bool operator != (const thing<N> &) const { return false; }
bool operator < (const thing<N> &) const { return false; }
bool operator > (const thing<N> &) const { return false; }
bool operator <= (const thing<N> &) const { return true; }
bool operator >= (const thing<N> &) const { return true; }
int operator - (const thing<N> &) const { return 0; }
// but explicitly delete ops for different N:
// (see https://*.com/questions/65468069)
template <int R> bool operator == (const thing<R> &) const = delete;
template <int R> bool operator != (const thing<R> &) const = delete;
template <int R> bool operator < (const thing<R> &) const = delete;
template <int R> bool operator > (const thing<R> &) const = delete;
template <int R> bool operator <= (const thing<R> &) const = delete;
template <int R> bool operator >= (const thing<R> &) const = delete;
template <int R> int operator - (const thing<R> &) const = delete;
// but if i don't delete <=> for differing template parameters then things
// like thing<0>() <=> thing<1>() will be allowed to compile because they'll
// be implicitly converted to an int. so i *have* to delete it when supported.
#if SPACESHIP_OPERATOR_IS_SUPPORTED
std::strong_ordering operator <=> (const thing<N> &) const = default;
template <int R> std::strong_ordering operator <=> (const thing<R> &) const = delete;
#endif
};
int main () {
thing<0> t0;
thing<1> t1;
(void)(t0 == t0); // line 39
//(void)(t0 == t1); // line 40
#if SPACESHIP_OPERATOR_IS_SUPPORTED
(void)(t0 <=> t0); // line 42
//(void)(t0 <=> t1); // line 43
#endif
}
所以,首先快速解释一下:
- 隐含的
operator int是一项要求。 - 比较运算符仅针对具有相同
N的thing<int N>s 定义。 - 必须显式删除不匹配的
Ns 的运算符,否则编译器将决定将operator int隐式应用于双方,并改用int比较 (see linked question)。 - 预期的行为是第 40 行和第 43 行(已标记)无法编译。
现在,我(认为)需要有条件地检查 operator <=> 支持的原因是:
- 代码需要编译为 C++14、17 和 20。
- 如果我根本不重载
<=>,那么像thing<0>() <=> thing<1>()这样的东西会被错误地编译(由于隐式转换为int;与其他运算符的情况相同)。换句话说:默认的operator <=>并不适用于所有情况,所以我不能任其发展。 - 如果我总是同时编写
<=>重载,则程序无法编译为 C++14 和 C++17,或者可能无法在 C++20 实现不完整的编译器上编译(尽管我没有遇到过这种情况)。
只要我手动设置SPACESHIP_OPERATOR_IS_SUPPORTED,上面的代码就满足所有要求,但我希望它是自动的。
所以,我的问题是:有没有办法在编译时检测对operator <=> 的支持,并有条件地启用代码(如果存在)?还是有其他方法可以使 C++14 到 20 的工作?
我处于预编译的心态,但如果有一些神奇的模板解决方案,那也可以。我真的很想要一个独立于编译器的解决方案,但至少我希望它能够在 GCC(5.x 及更高版本)和 MSVC(最好是 2015 年及更高版本)上运行。
【问题讨论】:
标签: c++ c++14 portability c++20 spaceship-operator