Using the this ptr in a constexpr expression

127 Views Asked by At

I have two types, one for define Natural numbers (positive integers >= 1) and other for define Integers (any whole numerical value, or integer).

Trying to implement the operator-(const Natural rhs), I would like to return an Integer instead of a Natural if the subtraction value is < 1. So this is my attempt:

[[nodiscard]] constexpr auto Natural::operator-(const Natural& rhs) const noexcept
    -> std::conditional_t<(_number > rhs.number() + 1), Natural, Integer>
{
    if constexpr(_number > rhs.number() + 1)
        return Natural(_number - rhs.number());
    else
        return Integer(static_cast<int>(_number) - static_cast<int>(rhs.number()));
}

But Clang-Tidy is telling me that is not allowed the use of the implicit this ptr in the constexpr branch, because the value is not known at compile time. That makes all the sense, but I don't know how to solve this situation with other tools than SFINAE, which I am trying to avoid because is usage is more complicated and the codebase should be the simpler as possible.

So, is there any way where I can solve this, and be able to discard one of the branches at compile time?

Note: Natural type is just a wrapper over an unsigned int

0

There are 0 best solutions below