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 10:29:51 +0000
On Mon, Nov 28, 2022 at 4:55 PM Gergely Nagy <gergely.nagy.alt_at_[hidden]> wrote:
>
> I think this would cause more harm than good.
>
> Can you explain what is the problem you want to slove that cannot be done any other way?


I want to be able to pass the name of a member function to a template
function as follows:

#include <type_traits> // is_class_v
#include <concepts> // derived_from
#include <istream> // istream

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;
    }
}

#include <iostream> // cout, endl
#include <string> // string
#include <typeinfo> // typeid

int main(void)
{
    std::string s;

    auto x = GetRelevantMethodPointer(&s);

    std::cout << typeid(x).name() << std::endl;
}

Received on 2022-11-29 10:30:03