Choosing between valid and invalid types

If we have a metafocus of a type template std::conditional, we can “select” types based on a logical compilation condition. For instance:

template < bool CONDITION, typename T, typename U >
struct conditional;

template < typename T, typename U > 
struct conditional < true, T, U >
{
    using type = T;
};

template < typename T, typename U > 
struct conditional < false, T, U >
{
    using type = U;
};

const bool whats_big = sizeof( int ) > sizeof( double );

using bigger_type = typename conditional<whats_big , int , double>::type;

My question is: Can I choose between a valid type and an invalid type?

Im currently implementing an event class. Events have a sender parameter and a variable number of args events:

template<typename SENDER , typename... ARGS>
class event;

Thus, functions with a type void(SENDER& , ARGS&...)can be used as event handlers. In this case, the handlers are called by passing a reference to the object that raised the event (sets the sender parameter).
On the other hand, I want the sender member functions to be event handlers, in other words, type functions void(SENDER::*)(ARGS&...).

, :

 using handler_type = typename conditional<std::is_class<SENDER>::value,void(SENDER::*)(ARGS&...) , void(SENDER& , ARGS&...)>::type;

, SENDER , ( ).

+2
1

:

template <bool, typename T, typename ...Args>
struct sender_chooser
{
    using type = void(*)(T &, Args &...);
};

template <typename T, typename ...Args>
struct sender_chooser<true, T, Args...>
{
    using type = void (T::*)(Args &...);
};

template <typename T, typename ...Args>
struct sender_type
{
    using type =
        typename sender_chooser<std::is_class<T>::value, T, Args...>::type;
};

:

sender_type<MySender, Arg1, Arg2, Arg3>::type

void (MySender::*)(Arg1 &, Arg2 &, Arg3 &), MySender - , void (*)(Sender &, Arg1 &, Arg2 &, Arg3 &).

(, .)

+8

Source: https://habr.com/ru/post/1543676/


All Articles