3

yesterday I came across a problem, where my metafunction didn't work as exspected. Then I commented out all (seemingly) unrelated code to find the problem. And it worked.

Here is my code complete code

#include <iostream>
#include <type_traits>

template <typename...> struct make_void { using type = void; };

template <typename... Ts> using void_t = typename make_void<Ts...>::type;

template <typename Functor, typename = void>
struct is_functor : std::false_type {};

template <typename Functor>
struct is_functor<Functor, void_t<decltype(&Functor::operator())>>
    : std::true_type {};

template <typename Callable,
          typename Signature = decltype(&Callable::operator())>
struct ExtractCallableRunTypeImpl;

template <typename Callable, typename R, typename... Args>
struct ExtractCallableRunTypeImpl<Callable, R (Callable::*)(Args...) const> {
  using Type = R(Args...);
};

template <typename Callable>
using ExtractCallableRunType =
    typename ExtractCallableRunTypeImpl<Callable>::Type;

template <typename Functor, typename SFINAE = void>
struct IsConvertibleToRunType : std::false_type {};

template <typename Callable>
struct IsConvertibleToRunType<Callable, void_t<decltype(&Callable::operator())>>
    : std::is_convertible<Callable, ExtractCallableRunType<Callable> *> {};


int main() {
  auto f = []() {};
  std::cout << IsConvertibleToRunType<decltype(f)>::value << std::endl;

  return 0;
}

Running this code will print "0". After commenting out the following portion of the code, it shows my expected behavior and prints out "1".

template <typename Functor, typename = void>
struct is_functor : std::false_type {};

template <typename Functor>
struct is_functor<Functor, void_t<decltype(&Functor::operator())>>
    : std::true_type {};

It seems to be somehow related to the use of void_t, but frankly can't wrap my head around why it has any influence on the code following. The is_functor metafunction is not used at all.

Here the demo http://rextester.com/JRQU76860 Run the demo, then comment out the marked portion of the code and run again.

Do you have any idea?

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141

0 Answers0