Is_function type trait for functors / function objects

Consider the following code snippet:

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

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

Output signal false.

No surprises here, as a functor Bardoes not qualify as a type of function. § 8.3.3. Functions [dcl.fct].

Now consider the following code snippet:

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

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

Note the parentheses after Bar. Output signal true.

How Bar()qualifies as a type of function?

I suppose this is the case of the most unpleasant parsing, but how can this be since it is in the list of arguments of the template?

+4
source share
2 answers

, , MVP, , Bar .

Bar foo();

Bar().

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

, :

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

, Bar std::is_function, . - . ( fooobar.com/questions/396964/...):

template<class F, class... T, typename = decltype(std::declval<F>()(std::declval<T>()...))> 
std::true_type  supports_test(const F&, const T&...);
std::false_type supports_test(...);

template<class> struct supports;
template<class F, class... T> struct supports<F(T...)> 
: decltype(supports_test(std::declval<F>(), std::declval<T>()...)){};

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

int main(){
    static_assert( supports<Bar()>::value == true , "");
    static_assert( supports<Bar(double)>::value == true , ""); // because of overload
    static_assert( supports<Bar(int)>::value == true , ""); // because of conversion
    static_assert( supports<Bar(std::string)>::value == false , "");
}
+1

Source: https://habr.com/ru/post/1618662/


All Articles