Date: Thu, 17 Apr 2025 10:37:05 +0100
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.
>
> > // 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.
Received on 2025-04-17 09:37:17