C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 17 Apr 2025 11:43:53 +0100
On Thu, Apr 17, 2025 at 10:54 AM Sebastian Wittmeier wrote:
>
> Can you also use inheritance with the first way? By specializing the template or disallowing for derived types.


No I don't think the first way can accommodate derived classes. In
order to adapt the first way to work with derived class, I think you'd
have to do something like the following:

    class IsArithmetic {};

    class MyString : IsArithmetic {
    public:
        MyString(char const*){}

        template<typename T>
        MyString &operator+=(T&&){ return *this; }
    };

    template <typename T>
    concept arithmetic = std::is_arithmetic_v<T> || std::is_base_of_v<
IsArithmetic, MyString >;

As you can see, there's maybe 3 or 4 or 5 ways of 'tagging' a class.
Which I think is why we should standardise it, and give it the feature
that you can specify whether the tag is inherited or not, and perhaps
be able to mark any given derived class as opting out of inheriting
tags from its base classes.

So maybe something like:

    class MyString {
        _Tag arithmetic;
    };

    class MyDerivedString : public MyString {}; // automatically has
the 'arithmetic' tag

    class MyOtherDerivedString : public notags MyString {}; //
doesn't inherit any tags from MyString

And then maybe consider allowing the addition of tag outside of the class:

    class MyString {};
    _Tag arithmetic >> MyString;

Received on 2025-04-17 10:44:06