簡體   English   中英

確定 C++ 標准算法 function 的返回類型,如 count_if

[英]Determine return type of C++ std algorithm function like count_if

如何像std::count_if一樣確定std:: function 的類型? 理想情況下,我想要以下內容

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)>{}
    )
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM