函子/函数对象的is_function类型特征

时间:2015-12-05 00:12:07

标签: c++ c++11 c++14 typetraits most-vexing-parse

考虑以下代码:

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

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

输出为false

由于仿函数Bar不符合函数类型§8.3.5函数[dcl.fct]

,因此没有任何意外。

现在考虑以下代码:

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

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

注意Bar之后的括号。输出为true

Bar()如何作为函数类型限定?

我的猜测是,这是一个最令人烦恼的解析的情况,但它怎么可能因为它在模板参数列表中呢?

2 个答案:

答案 0 :(得分:4)

好吧,我不认为它是MVP,它只是返回Bar并且不接受任何参数的函数类型。

这就是这个功能

Bar foo();

的类型为Bar()

很自然地,std::is_function<Bar()>::valuetrue

它将是相同的:

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

答案 1 :(得分:1)

验证Bar是否可调用std::is_function无效。要做到这一点,你需要做点别的事情。基于通用代码(取自https://stackoverflow.com/a/18603716/225186):

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 , "");
}
相关问题