joboccara / NamedType

Implementation of strong types in C++
MIT License
769 stars 84 forks source link

Include unary operators in Subtractable/Addable skills #34

Closed knatten closed 4 years ago

knatten commented 4 years ago

Unary minus is needed to make user defined literals work nicely, since the minus is not a part of the literal, but rather applied as a unary operator afterwards. For instance:

using DegreesCelcius =
    fluent::NamedType<double, struct DegreesCelciusTag, fluent::Subtractable>;

DegreesCelcius operator"" _C(long double degreesCelcius)
{
    return DegreesCelcius(degreesCelcius);
}

DegreesCelcius minimumValue{-45._C};

The last line will first construct a DegreesCelcius{45.}, and then apply operator- to it.

I also included unary plus in Addable for symmetry, even if it's a lot less useful than unary minus.

(The compilation error in main.cpp is also fixed in #30, but I included it here as well so it's possible to run tests.)

joboccara commented 4 years ago

Thanks for the PR! I clearly see the point of adding those operators. To give more flexibility, what would you think of breaking down the skills into UnarySubtractable and BinarySubtractable, with Subtractable being the combination of the two? I'll accept your PR, and break down Subtractable into two. Let me know what you think!

joboccara commented 4 years ago

Fyi here is the commit to break them down: https://github.com/joboccara/NamedType/commit/d7828c6295863ff2f4af021682a1778807f98746

knatten commented 4 years ago

I like that idea! Thanks for accepting the PR.