Side effect of one meta function on another meta function in C ++?

I ran into an issue yesterday where my metafunction was not working as expected. I then commented out all (apparently) 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 next piece of 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 have something to do with the use of void_t, but frankly can't get my head around why this affects the following code. The is_functor metafocus is not used at all.

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

Do you have any ideas?

+3


source to share





All Articles