C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Concept Overloading in C++

From: Gašper Ažman <gasper.azman_at_[hidden]>
Date: Tue, 19 Dec 2023 11:26:36 +0000
Concepts were specifically designed to *not* be specializable. That was in
the design intent. I'm afraid if you want to do anything in that direction,
the road will be very, very difficult and require truly extraordinary
reasons for change. I wouldn't consider it a prudent time investment.

On Tue, Dec 19, 2023, 09:04 Richard Hodges via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> I think you can achieve it like this?
>
> template < class... Ts >
> auto
> is_summable_test(Ts...);
>
> template < class A, class B >
> auto
> is_summable_test(A a, B b)
> {
> return a + b;
> };
>
> template < class A >
> auto
> is_summable_test(A a)
> {
> return a + a;
> };
>
> template < class... Ts >
> concept is_summable = requires(Ts... ts) { is_summable_test(ts...); };
>
> static_assert(is_summable< int, int >);
> static_assert(is_summable< int >);
>
> On Tue, 19 Dec 2023 at 09:13, JIMMY HU via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> Hi,
>>
>> The meaning of *function overloading* is that two or more functions can
>> have the same name but different parameters. In C++20, concept
>> <https://en.cppreference.com/w/cpp/language/constraints> has been
>> introduced. I think that the same logic should be applied to concepts in
>> C++: *two or more concepts can have the same name but different number
>> of parameters, and the usage of concept depends on the number of given
>> parameters.*
>>
>> *Example*:
>> template<typename T>
>> concept is_summable = requires(T x) { x + x; }; // one
>> parameter version
>>
>> // error: redefinition of concept 'is_summable' with different template
>> parameters or requirements
>> template<typename T>
>> concept is_summable = requires(T x, T y) { x + y; }; // two
>> parameter version
>>
>> template<is_summable T> // the
>> usage of one parameter version
>> void foo(T input)
>> {
>>
>> }
>>
>> template<class T1, class T2>
>> requires(is_summable<T1, T2>) // the
>> usage of two parameter version
>> {
>>
>> }
>>
>> --
>> Jimmy Hu
>>
>> Dec. 19, 2023
>>
>> Email: s103360021_at_[hidden]
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-12-19 11:26:48