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
c ++ visual-c ++ templates


source to share


No one has answered this question yet

See similar questions:

18
void_t doesn't work on Visual Studio 2015

or similar:

8499
What is the "->" operator in C ++?
4247
The definitive C ++ guide and book list
3076
What are the differences between a pointer variable and a reference variable in C ++?
1783
C ++ 11 introduced a standardized memory model. What does it mean? And how will this affect C ++ programming?
1709
How can I profile C ++ code running on Linux?
1675
Why is reading lines from stdin much slower in C ++ than Python?
1475
What is the effect of extern "C" in C ++?
1455
Easiest way to convert int to string in C ++
1138
Why do we need virtual functions in C ++?
366
Pretty printable STL STL containers



All Articles
Loading...
X
Show
Funny
Dev
Pics