提问者:小点点

确定C++std算法函数(如count_if)的返回类型


如何确定像std::count_if这样的std::函数的类型?理想情况下,我想要以下内容

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'

共1个答案

匿名用户

最简单的方法就是使用decltype,如下所示:

using type = decltype(
    std::count_if(
        std::declval<const char*>(),
        std::declval<const char*>(),
        std::function<bool(const char)>{}
    )
);

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++std|算法|函数|count_if|返回|类型)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?