std::reference_wrapper<T>::operator()
來自 cppreference.com
< cpp | utility | functional | reference wrapper
template< class... ArgTypes > typename std::result_of<T&(ArgTypes&&...)>::type |
(C++11 起) (C++17 前) |
|
template< class... ArgTypes > std::invoke_result_t<T&, ArgTypes...> |
(C++17 起) (C++20 起為 constexpr) |
|
如同以 INVOKE(get()
, std::forward<ArgTypes>(args)...) 呼叫所儲存的可呼叫 (Callable) 物件。僅當被儲存的引用指向可呼叫 (Callable) 物件時,此函式才可用。
T
必須是完整型別。
目錄 |
[編輯] 引數
args | - | - 傳遞給被呼叫函式的引數 |
[編輯] 返回值
被呼叫函式的返回值。
[編輯] 異常
可能丟擲實現定義的異常。 |
(C++11 起) (C++17 前) |
noexcept 規範:
noexcept(std::is_nothrow_invocable_v<T&, ArgTypes...>) |
(C++17 起) |
[編輯] 示例
執行此程式碼
#include <functional> #include <iostream> void f1() { std::cout << "reference to function called\n"; } void f2(int n) { std::cout << "bind expression called with " << n << " as the argument\n"; } int main() { std::reference_wrapper<void()> ref1 = std::ref(f1); ref1(); auto b = std::bind(f2, std::placeholders::_1); auto ref2 = std::ref(b); ref2(7); auto c = []{ std::cout << "lambda function called\n"; }; auto ref3 = std::ref(c); ref3(); }
輸出
reference to function called bind expression called with 7 as the argument lambda function called
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3764 | C++17 | operator() 不是 noexcept 的 | 傳播 noexcept |
[編輯] 參閱
訪問所儲存的引用 (公開成員函式) |