Skip to content

Poll: How to constrain operations in a class template? #29

Closed
@mpusz

Description

@mpusz

The more I think about the "number" concept (#28) the more doubts I have. I see 2 solutions here:

  • Option 1

    template<Unit U, Number Rep = double>
    class quantity {
    public:
      template<Number Value>
      constexpr quantity& operator%=(const Value& rhs);
      // ...
    };

    In this case, Rep has to satisfy all of the requirements of the Number concepts. It means that even if the user is never going to use operator %= on a quantity his/her representation type has to provide it or the quantity class template instantiation will fail.

  • Option 2

    template<typename T>
    concept UnitRep = std::regular<T> && std::totally_ordered<T>;
    
    template<Unit U, UnitRep Rep = double>
    class quantity {
    public:
      template<Number Value>
      constexpr quantity& operator%=(const Value& rhs)
          requires requires(Rep v) { { v %= rhs } -> std::same_as<Rep&>; };
      // ...
    };

    In this case, the user will be able to instantiate the quantity class template for every "sane" type and will be able to use only those arithmetic operations that are supported by the underlying Rep type.

Which option do you prefer?


(please vote only once by clicking on the correct bar above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions