許多基礎庫都要求調用方傳遞一個可調用的實體(entity)。例如:一個描述如何排序的函數、一個如何hash的函數。通常用callback
來描述這種用法。在C++中有如下幾種形式能夠實現callback,它們均可以被當作函數參數傳遞並能夠直接使用相似f(...)
的方式調用:ios
operator()
的類(有時被叫作functors
),包括lambdas.C++使用callable type
來描述上面這些類型。好比,一個能夠被調用的對象稱做callable object
,咱們使用callback
來簡化這個稱呼。c++
編寫泛型代碼會由於這個用法的存在而可擴展不少。git
例如一個for_each的實現:github
template <typename Iter, typename Callable> void foreach (Iter current, Iter end, Callable op) { while (current != end) { // as long as not reached the end op(*current); // call passed operator for current element ++current; // and move iterator to next element } }
使用不一樣的Function Objects
來調用這個模板:express
// a function to call: void func(int i) { std::cout << "func() called for: " << i << '\n'; } // a function object type (for objects that can be used as functions): class FuncObj { public: void operator()(int i) const { // Note: const member function std::cout << "FuncObj::op() called for: " << i << '\n'; } }; int main(int argc, const char **argv) { std::vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19}; foreach (primes.begin(), primes.end(), func); // range function as callable (decays to pointer) foreach (primes.begin(), primes.end(), &func); // range function pointer as callable foreach (primes.begin(), primes.end(), FuncObj()); // range function object as callable foreach (primes.begin(), primes.end(), // range lambda as callable [](int i) { std::cout << "lambda called for: " << i << '\n'; }); return 0; }
解釋一下:數組
foreach (primes.begin(), primes.end(), func);
按照值傳遞時,傳遞函數會decay爲一個函數指針。foreach (primes.begin(), primes.end(), &func);
這個比較直接,直接傳遞了一個函數指針。foreach (primes.begin(), primes.end(), FuncObj());
這個是上面說過的functor
,一個重載了operator()
的類。因此,當調用op(*current);
時,實際是在調用op.operator()(*current);
. ps. 若是不加函數聲明後面的const,在某些編譯器中可能會報錯。上面沒有提到一個場景 : 成員函數。由於調用非靜態成員函數的方式是object.memfunc(. . . )
或ptr->memfunc(. . . )
,不是統一的function-object(. . . )
。函數
幸運的是,從C++17起,C++提供了std::invoke<>()
來統一全部的callback形式:this
template <typename Iter, typename Callable, typename... Args> void foreach (Iter current, Iter end, Callable op, Args const &... args) { while (current != end) { // as long as not reached the end of the elements std::invoke(op, // call passed callable with args..., // any additional args *current); // and the current element ++current; } }
那麼,std::invoke<>()
是怎麼統一全部callback形式的呢?
注意,咱們在foreach中添加了第三個參數:Args const &... args
. invoke是這麼處理的:lua
使用:設計
// a class with a member function that shall be called class MyClass { public: void memfunc(int i) const { std::cout << "MyClass::memfunc() called for: " << i << '\n'; } }; int main() { std::vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19}; // pass lambda as callable and an additional argument: foreach ( primes.begin(), primes.end(), // elements for 2nd arg of lambda [](std::string const &prefix, int i) { // lambda to call std::cout << prefix << i << '\n'; }, "- value: "); // 1st arg of lambda // call obj.memfunc() for/with each elements in primes passed as argument MyClass obj; foreach (primes.begin(), primes.end(), // elements used as args &MyClass::memfunc, // member function to call obj); // object to call memfunc() for }
注意在callback是成員函數的狀況下,是如何調用foreach的。
std::invoke()
的一個場景用法是:包裝一個函數調用,這個函數能夠用來記錄函數調用日誌、測量時間等。
#include <utility> // for std::invoke() #include <functional> // for std::forward() template<typename Callable, typename... Args> decltype(auto) call(Callable&& op, Args&&... args) { return std::invoke(std::forward<Callable>(op), std::forward<Args>(args)...); // passed callable with any additional args }
一個須要考慮的事情是,如何處理op的返回值並返回給調用者:
template<typename Callable, typename... Args> decltype(auto) call(Callable&& op, Args&&... args)
這裏使用decltype(auto)
(從C++14起)(decltype(auto)
的用法能夠看以前的文章 : c++11-17 模板核心知識(九)—— 理解decltype與decltype(auto))
若是想對返回值作處理,能夠聲明返回值爲decltype(auto)
:
decltype(auto) ret{std::invoke(std::forward<Callable>(op), std::forward<Args>(args)...)}; ... return ret;
可是有個問題,使用decltype(auto)
聲明變量,值不容許爲void,能夠針對void和非void分別進行處理:
#include <functional> // for std::forward() #include <type_traits> // for std::is_same<> and invoke_result<> #include <utility> // for std::invoke() template <typename Callable, typename... Args> decltype(auto) call(Callable &&op, Args &&... args) { if constexpr (std::is_same_v<std::invoke_result_t<Callable, Args...>, void>) { // return type is void: std::invoke(std::forward<Callable>(op), std::forward<Args>(args)...); ... return; } else { // return type is not void: decltype(auto) ret{ std::invoke(std::forward<Callable>(op), std::forward<Args>(args)...)}; ... return ret; } }
std::invoke_result<>
只有從C++17起才能使用,C++17以前只能用typename std::result_of<Callable(Args...)>::type
.
這個技術不少人應該很熟悉,這裏不細說了。
#include <type_traits> template <typename T> class C { // ensure that T is not void (ignoring const or volatile): static_assert(!std::is_same_v<std::remove_cv_t<T>, void>, "invalid instantiation of class C for void type"); public: template <typename V> void f(V &&v) { if constexpr (std::is_reference_v<T>) { ... // special code if T is a reference type } if constexpr (std::is_convertible_v<std::decay_t<V>, T>) { ... // special code if V is convertible to T } if constexpr (std::has_virtual_destructor_v<V>) { ... // special code if V has virtual destructor } } };
這裏,咱們使用type_traits來進行不一樣的實現。
可使用std::addressof<>()
獲取對象或者函數真實的地址, 即便它重載了operator &
. 不過這種狀況不是很常見。當你想獲取任意類型的真實地址時,推薦使用std::addressof<>():
template<typename T> void f (T&& x) { auto p = &x; // might fail with overloaded operator & auto q = std::addressof(x); // works even with overloaded operator & ... }
好比在STL vector中,當vector須要擴容時,遷移新舊vector元素的代碼:
{ for (; __first != __last; ++__first, (void)++__cur) std::_Construct(std::__addressof(*__cur), *__first); return __cur; } template <typename _T1, typename... _Args> inline void _Construct(_T1 *__p, _Args &&... __args) { ::new (static_cast<void *>(__p)) _T1(std::forward<_Args>(__args)...); //實際copy(或者move)元素 }
這裏使用std::addressof()
獲取新vector當前元素的地址,而後進行copy(或move)。能夠看以前寫的c++ 從vector擴容看noexcept應用場景
std::declval
能夠被視爲某一特定類型對象引用的佔位符。它不會建立對象,經常和decltype和sizeof搭配使用。所以,在不建立對象的狀況下,能夠假設有相應類型的可用對象,即便該類型沒有默認構造函數或該類型不能夠建立對象。
注意,declval只能在unevaluated contexts中使用。
一個簡單的例子:
class Foo; //forward declaration Foo f(int); //ok. Foo is still incomplete using f_result = decltype(f(11)); //f_result is Foo
如今若是我想獲取使用int調用f()後返回的類型是什麼?是decltype(f(11))
?看起來怪怪的,使用declval看起來就很明瞭:
decltype(f(std::declval<int>()))
還有就是以前c++11-17 模板核心知識(一)—— 函數模板中的例子)——返回多個模板參數的公共類型:
template <typename T1, typename T2, typename RT = std::decay_t<decltype(true ? std::declval<T1>() : std::declval<T2>())>> RT max(T1 a, T2 b) { return b < a ? a : b; }
這裏在爲了不在?:
中不得不去調用T1 和T2 的構造函數去建立對象,咱們使用declval來避免建立對象,並且還能夠達到目的。ps. 別忘了使用std::decay_t,由於declval返回的是一個rvalue references. 若是不用的話,max(1,2)
會返回int&&
.
最後看下官網的例子:
#include <utility> #include <iostream> struct Default { int foo() const { return 1; } }; struct NonDefault { NonDefault() = delete; int foo() const { return 1; } }; int main() { decltype(Default().foo()) n1 = 1; // type of n1 is int // decltype(NonDefault().foo()) n2 = n1; // error: no default constructor decltype(std::declval<NonDefault>().foo()) n2 = n1; // type of n2 is int std::cout << "n1 = " << n1 << '\n' << "n2 = " << n2 << '\n'; }
template<typename T> void f (T&& t) // t is forwarding reference { g(std::forward<T>(t)); // perfectly forward passed argument t to g() }
或者轉發臨時變量,避免無關的拷貝開銷:
template<typename T> void foo(T x) { auto&& val = get(x); ... // perfectly forward the return value of get() to set(): set(std::forward<decltype(val)>(val)); }
template<typename T> void tmplParamIsReference(T) { std::cout << "T is reference: " << std::is_reference_v<T> << '\n'; } int main() { std::cout << std::boolalpha; int i; int& r = i; tmplParamIsReference(i); // false tmplParamIsReference(r); // false tmplParamIsReference<int&>(i); // true tmplParamIsReference<int&>(r); // true }
這點也不太常見,在前面的文章c++11-17 模板核心知識(七)—— 模板參數 按值傳遞 vs 按引用傳遞提到過一次。這個會改變強制改變模板的行爲,即便模板的設計者一開始不想這麼設計。
我沒怎麼見過這種用法,並且這種用法有的時候會有坑,你們瞭解一下就行。
可使用static_assert禁止這種用法:
template<typename T> class optional { static_assert(!std::is_reference<T>::value, "Invalid instantiation of optional<T> for references"); … };
首先引入一個概念:incomplete types. 類型能夠是complete或者incomplete,incomplete types包含:
能夠理解incomplete types爲只是定義了一個標識符可是沒有定義大小。例如:
class C; // C is an incomplete type C const* cp; // cp is a pointer to an incomplete type extern C elems[10]; // elems has an incomplete type extern int arr[]; // arr has an incomplete type ... class C { }; // C now is a complete type (and therefore cpand elems no longer refer to an incomplete type) int arr[10]; // arr now has a complete type
如今回到Defer Evaluations的主題上。考慮以下類模板:
template<typename T> class Cont { private: T* elems; public: ... };
如今這個類可使用incomplete type,這在某些場景下很重要,例如鏈表節點的簡單實現:
struct Node { std::string value; Cont<Node> next; // only possible if Cont accepts incomplete types };
可是,一旦使用一些type_traits,類就再也不接受incomplete type:
template <typename T> class Cont { private: T *elems; public: ... typename std::conditional<std::is_move_constructible<T>::value, T &&, T &>::type foo(); };
std::conditional
也是一個type_traits,這裏的意思是:根據T是否支持移動語義,來決定foo()返回T &&
仍是T &
.
可是問題在於,std::is_move_constructible
須要它的參數是一個complete type. 因此,以前的struct Node這種聲明會失敗(不是全部的編譯器都會失敗。其實這裏我理解不該該報錯,由於按照類模板實例化的規則,成員函數只有用到的時候才進行實例化)。
咱們可使用Defer Evaluations來解決這個問題:
template <typename T> class Cont { private: T *elems; public: ... template<typename D = T> typename std::conditional<std::is_move_constructible<T>::value, T &&, T &>::type foo(); };
這樣,編譯器就會直到foo()被complete type的Node調用時才實例化。
(完)
朋友們能夠關注下個人公衆號,得到最及時的更新: