Search code examples
c++c++11c++14type-traitsmost-vexing-parse

is_function type trait for functors/function-objects


Consider the following piece of code:

struct Bar { 
  void operator()() {}
};

int main() {
  std::cout << std::boolalpha << std::is_function<Bar>::value << 
}

The output is false.

No surprises here since functor Bar does not qualify as a function type §8.3.5 Functions [dcl.fct].

Now consider the following piece of code:

struct Bar { 
  void operator()() {}
};

int main() {
  std::cout << std::boolalpha << std::is_function<Bar()>::value << std::endl;
                                                     ^^
}

Notice the parentheses after Bar. The output is true.

How is Bar() qualified as a function type?

My guess is that it's a case of most vexing parse, but how can it be since it's in the template argument list?


Solution

  • Well, I don't think it is a MVP, it is simply the type of a function that returns Bar and does not take any arguments.

    That is this function

    Bar foo();
    

    has type Bar().

    So naturally, std::is_function<Bar()>::value is true.

    It would be the same that:

    typedef Bar F();
    std::cout << std::is_function<F>::value << std::endl;