使用 boost::operators 为模板类定义运算符

发布于 2025-01-07 10:26:47 字数 521 浏览 1 评论 0原文

我有一个如下所示的类:

template<int DIGITS, int FRACTIONS>
class decimal{

    bool operator<(const int& rhs) const;

    template<int RDIGITS, int RFRACTIONS>
    bool operator<(const decimal<RDIGITS, RFRACTIONS>& rhs) const;
}

我应该能够为整数添加相关的比较运算符

class decimal : boost::less_than_comparable<fixed_decimal<DIGITS, DECSIZE>, int>{ }

但如何将 less_than_comparable 与其他十进制模板一起使用? 我希望能够比较十进制<10,5>和例如,十进制<6,4>。

I've got a class which looks like this:

template<int DIGITS, int FRACTIONS>
class decimal{

    bool operator<(const int& rhs) const;

    template<int RDIGITS, int RFRACTIONS>
    bool operator<(const decimal<RDIGITS, RFRACTIONS>& rhs) const;
}

I should be able to add related comparison operators for ints with

class decimal : boost::less_than_comparable<fixed_decimal<DIGITS, DECSIZE>, int>{ }

But how can I use less_than_comparable with other decimal templates?
I'd like to be able to compare decimal<10,5> with decimal<6,4>, for example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2025-01-14 10:26:47

我认为由于 C++ 编译器扩展模板的方式,这是不可能的。您必须为每个 组合定义运算符。在您的位置,我将采取动态方法并使这两个参数成为您的类的成员变量而不是模板参数。

I think this is not possible due to the way C++ compilers expand templates. You'd have to define operators for each <DIGITS, DECSIZE>-combination. In your place, I'd take a dynamic approach and make those two parameters member variables of your class instead of template parameters.

雪若未夕 2025-01-14 10:26:47

对于返回固定类型的运算符,例如 bool,您可以手动实现运算符:(

template <int N1, unsigned D1, int N2, unsigned D2>
bool operator <(fraction<N1, D1>, fraction<N2, D2>) {
    // Normalisation omitted, using floating point arithmetic
    return (static_cast<float>(N1) / D1) < (static_cast<float>(N2) / D2);
}

当然,有很多更聪明的方法来进行比较。)

但是一旦您涉足以下领域更复杂的算术表达式,例如运算符 +,这不再那么容易完成,因为返回类型取决于(目前未知)模板参数。

可以在C++11中执行如下操作:

template <int N1, unsigned D, int N2>
constexpr auto operator +(fraction<N1, D>, fraction<N2, D>)
    -> fraction<N1 + N2, D>
{
    return fraction<N1 + N2, D>();
}

...并调用:

fraction<1, 2> a;
fraction<3, 2> b;
auto c = a + b;

如果您不能使用C++11,那么您需要使用模板元函数而不是普通函数/重载运算符。

For operators that return a fixed type such as bool, you can implement the operators manually:

template <int N1, unsigned D1, int N2, unsigned D2>
bool operator <(fraction<N1, D1>, fraction<N2, D2>) {
    // Normalisation omitted, using floating point arithmetic
    return (static_cast<float>(N1) / D1) < (static_cast<float>(N2) / D2);
}

(Of course, there are much cleverer ways to do the comparison.)

But as soon as you foray into the realms of more complex arithmetic expressions such as operator + this can no longer be done as easily, since the return type depends on (as of yet unknown) template arguments.

You can do it in C++11 as follows:

template <int N1, unsigned D, int N2>
constexpr auto operator +(fraction<N1, D>, fraction<N2, D>)
    -> fraction<N1 + N2, D>
{
    return fraction<N1 + N2, D>();
}

… and to invoke:

fraction<1, 2> a;
fraction<3, 2> b;
auto c = a + b;

If you cannot use C++11, then you need to use template metafunctions instead of normal functions / overloaded operators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文