C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::arithmetic (concept)

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Thu, 17 Apr 2025 11:48:14 +0200
Can you also use inheritance with the first way? By specializing the template or disallowing for derived types.   -----Ursprüngliche Nachricht----- Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Do 17.04.2025 11:37 Betreff:Re: [std-proposals] std::arithmetic (concept) An:std-proposals_at_[hidden]; CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>; On Wed, Apr 16, 2025 at 12:04 PM Sebastian Wittmeier wrote: > > > // customization point as per [namespace.std], with semantic requirements > > template <typename T> > > constexpr bool enable_arithmetic = false; > > > > template <typename T> > > concept arithmetic = is_arithmetic_v<T> || enable_arithmetic<T>; > > > I think that was meant as customizable concept (instead of tags). > One can specialize the variable template for custom types and also specify, whether > the variable template expects the exact type or also descendants. Oh I see. If we start out with the following code:    template<arithmetic Arith>    void Func(Arith &arg)    {        arg += 5u;    }    int main(void)    {        int n = 72;        Func(n);        MyString s("Hello");        Func(s);    } There are two main ways of getting this to work. Here's the first way:    template <typename T>    constexpr bool enable_arithmetic = false;    class MyString {    public:        MyString(char const*){}        template<typename T>        MyString &operator+=(T&&){ return *this; }    };    template<>    constexpr bool enable_arithmetic<MyString> = true;    template <typename T>    concept arithmetic = std::is_arithmetic_v<T> || enable_arithmetic<T>; And here's the second way:    class MyString {    public:        static constexpr bool tag_arithmetic = true;        MyString(char const*){}        template<typename T>        MyString &operator+=(T&&){ return *this; }    };    template <typename T>    concept arithmetic = std::is_arithmetic_v<T> || T::tag_arithmetic; The first way does not require an alteration to the original class definition, and so you could "add a tag" to a class in a third party library header, something like as follows:    #include "python.hpp"    template<>    constexpr bool enable_arithmetic< python::BigNumber > = true; The second way keeps it all inside the original class definition. The 'tag' could be in the form of a "static constexpr bool" or it could be a type. If it's a type, then there is a strategy to make the tag inherited (or not inherited) by derived classes (i.e. compare the tag type to the Derived class type). Rather than have different programmers have different strategies for how they should tag a class, I think it would be better to standardise it. -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-04-17 09:54:35