C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Extreme Template Parameter Possibilities

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 29 Nov 2022 12:09:28 +0000
On Tue, Nov 29, 2022 at 10:29 AM Frederick Virchanza Gotham
<cauldwell.thomas_at_[hidden]> wrote:
>
> template<text K, typename T>
> constexpr auto GetRelevantMethodPointer(T const *const p)
> {
> if constexpr ( false == std::is_class_v<T> ) return
> static_cast<void(*)(void)>(nullptr);
> else if constexpr ( std::derived_from<T,std::istream> ) return &T::read;
> else if constexpr ( requires { &T::put; } ) return &T::put;
> else
> {
> return &T::K;
> }
> }


This wasn't a very good example. A better example would be where we
want to find a member of the same name in a different class. See line
#7 in the following:

00: template<typename T>
01: constexpr auto GetRelevantMethodPointer(T const *const p)
02: {
03: if constexpr ( false == std::is_class_v<T> ) return
04: static_cast<void(*)(void)>(nullptr);
05: else if constexpr ( std::derived_from<T,std::istream> )
return &T::read;
06: else if constexpr ( requires { &T::put; } ) return &T::put;
07: else if constexpr ( requires { &SomeOtherClass::K; } ) return
&SomeOtherClass::K;
08: else
09: {
10: return &T::K;
11: }
12: }

Received on 2022-11-29 12:09:40