ericniebler / meta

A tiny metaprogramming library
Boost Software License 1.0
302 stars 47 forks source link

Request for interest in the equivalent of std::is_callable<Fn(Args...), R> in Meta #35

Open viboes opened 8 years ago

viboes commented 8 years ago

Hi,

I need a type traits to check if a Callable Fn can be invoked with some specific parameters Args...and if the result is convertible to a type R. Something in line with std::is_callable but using meta::invoke instead of std::invoke.

As meta::is_callable is already used what would be the name of this new type trait?

I would prefer to rename the current meta::is_callable to something else ut I don't have a good proposal. I use in my own code is_type_constructor as Meta-Callables are are functions that construct types.

  template <class, R = void>
    struct is_callable; // not defined
  template <class Fn, class ...Args, class R>
    struct is_callable<Fn(Args...), R>;
  template <class Sig, R = void> 
    constexpr bool is_callable_v = is_callable <Sig, R>::value;

If TC::template invoke<Xs...> is well formed then if Ris void then true_type else std::is_convertible<meta::invoke_t<Xs...>, R> else false_type.

No conversion check is performed when R is void.

BTW, in order to avoid confusion between std Callables and meta Callables what do you think to rename Callable to Meta-Callable?

ericniebler commented 8 years ago

I could add is_callable_with<C, Args...>, which would be an alias for has_type<lazy::invoke<C, Args...>>. Why you think that a test for is_convertible is appropriate for metaprograms?

BTW, in order to avoid confusion between std Callables and meta Callables what do you think to rename Callable to Meta-Callable?

Well, once we have concepts in the language, the concept is called Meta-Callable -- or rather, meta::Callable. I suppose when describing things in English I could use Meta-Callable as a disambiguator.

viboes commented 8 years ago

Originally I called it also is_callable_with, but I changed it to follow std::is_callable style. I added also the is_convertible check in order to be as close as possible to std::is_callable. I believe it doesn't hurt.

viboes commented 8 years ago

Please, take a look at https://github.com/boostorg/hana/issues/277.

Paul has showed me that it is not the same is_callable<F(int[3])> than is_callable<F, int[3]>. The first is deduced as is_callable<F(int*)> :(

ericniebler commented 8 years ago

That's why I suggested is_callable_with<C, Args...>. And I think checking for is_convertible is actively hostile to metaprograms. At compile time, one type is not really "convertible" to another.