确定像count_if这样的C++std算法函数的返回类型
如何确定std::函数的类型,如std::count_if?理想情况下,我想要以下内容
using ret = std::invoke_result_t<std::count_if, const char*, const char*,
std::function<bool(const char)>>;
但由于多种原因,这不起作用。我可以通过以下方式接近:
using f = std::function<bool(const char)>;
using ret = std::invoke_result_t<std::count_if, const char*, const char*, f>;
但这还不够。
错误:
expected a type, got 'std::count_if'
回答
最简单的方法是像这样使用 decltype:
using type = decltype(
std::count_if(
std::declval<const char*>(),
std::declval<const char*>(),
std::function<bool(const char)>{}
)
);