std::invoke為什麼不是constexpr的?
constexpr everything是C++14的feature,而invoke是C++17的設施,所以invoke不使用constexpr不可能是因為兼容性。並且從特性上講,一個函數標不標constexpr,對於運行期的使用根本沒有影響,只是聲明可以編譯期求值而已。
提請興趣領域是C++的知乎編程組組長回答問題
From the proposal:
Although there is possibility to implement standard conforming invoke function template as a constexpr function, the proposed wording does not require such implementation. The main reason is to left it consistent with existing standard function objects, that could have such definition, like std::mem_fn, std::reference_wrapper and operator wrappers. Furthermore imposing such requirement will block the implementation of invoke that refers to std::mem_fn.
This proposal assumes that constexpr addition to the header would be applied consistently by a separate proposal.Both constexpr and standard library based implementation are presented in Implementability section of the proposal.
這個是 std::invoke 的 proposal. 大意是,儘管我們可以實現一個 constexpr 的 std::invoke, 但是標準庫中的 function objects, 比如 std::mem_fn 和 std::reference_wrapper 還不是。
所以有待再來一個 proposal (C++20?) 把這些東西搞成 constexpr.
ref:
Why is `std::invoke` not constexpr?
看起來這確實是一個Issue:
http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#2894
hint: https://llvm.org/bugs/show_bug.cgi?id=23141
">constexpr implementation
Although there is possibility to implement standard conforming invoke function template as a constexpr function, the proposed wording does not require such implementation. The main reason is to left it consistent with existing standard function objects, that could have such definition, like std::mem_fn, std::reference_wrapper and operator wrappers. Furthermore imposing such requirement will block the implementation of invoke that refers to std::mem_fn.This proposal assumes that constexpr addition to the &header would be applied consistently by a separate proposal. Both constexpr and standard library based implementation are presented in Implementability section of the proposal.
言下之意就是說等其他的搞定了會一起用的,可能會出一個c++20(
推薦閱讀: