剖析std::function介面與實現
目錄
前言
一、std::function的原理與介面
1.1 std::function是函式包裝器
1.2 C++注重執行時效率
1.3 用函式指標實現多型
1.4 std::function的介面
二、std::function的實現
2.1 型別系統
2.1.1 異常類
2.1.2 資料儲存
2.1.3 輔助類
2.1.4 記憶體管理基類
2.1.5 仿函式呼叫
2.1.6 介面定義
2.1.7 型別關係
2.2 方法的功能與實現
2.2.1 多型性的體現
2.2.2 本地函式物件
2.2.3 heap函式物件
2.2.4 兩種儲存結構如何統一
2.2.5 根據形式區分仿函式型別
2.2.6 實現組裝成介面
後記
附錄
前言
為什麼要剖析 std::function 呢?因為筆者最近在做一個 std::function 向微控制器系統的移植與擴充套件。
後續還會有 std::bind 等標準庫其他部分的移植。
一、std::function的原理與介面
1.1 std::function是函式包裝器
std::function ,能儲存任何符合模板引數的函式物件。換句話說,這些擁有一致引數型別、相同返回值型別(其實不必完全相同)的函式物件,可以由 std::function 統一包裝起來。函式物件的大小是任意的、不能確定的,而C++中的型別都是固定大小的,那麼,如何在一個固定大小的型別中儲存任意大小的物件呢?
實際上問題還不止儲存那麼簡單。儲存了函式物件,必定是要在某一時刻呼叫;函式物件不是在建立的時候呼叫,這個特性成為延遲呼叫;函式物件也是物件,需要處理好構造、拷貝、移動、析構等問題——這些也需要延遲呼叫,但總不能再用 std::function 來解決吧?
既然 std::function 能儲存不同型別的函式物件,可以說它具有多型性。C++中體現多型性的主要是虛擬函式,繼承與多型這一套體制是可以解決這個問題的。相關資料[1] [2]中的實現利用了繼承與多型,相當簡潔。
1.2 C++注重執行時效率
利用繼承與多型,我們可以讓編譯器幫我們搞定函式物件的析構。就這種實現而言,這是簡潔而有效的方法。然而這種實現需要動態記憶體,在一些情況下不划算,甚至完全沒有必要。C++11引入了lambda表示式,其本質也是函式物件。這個物件有多大呢?取決於捕獲列表。你寫lambda會捕獲多少東西?很多情況下就只是一對方括號而已吧。在這種情況下,lambda表示式的物件大小隻有1位元組(因為不能是0位元組),你卻為了這沒有內容的1位元組要呼叫動態記憶體的函式?C++注重執行時效率,這種浪費是不能接受的。
如何避免這種浪費呢?你也許會說我檢查傳入的物件是不是1位元組的空型別。且不論這個trait怎麼實現,函式指標、捕獲一個int的lambda等型別都聲稱自己是trivial的小物件,也不應該分配到heap中去。
之前說過,std::function 的大小是固定的,但是這個大小是可以自己定的。我們可以在 std::function 的類定義中加入一個空白的、大小適中的field,用在存放這些小物件,從而避免這些情況下的動態記憶體操作。同時,既然有了這片空間,也就不需要看傳入的物件是不是1位元組的空型別了。
而對於更大的型別,雖然這個field不足以存放函式物件,但足以存放一個指標,這種分時複用的結構可以用union來實現。這種小物件直接儲存、大物件在heap上開闢空間並存儲指標的方法,稱為small object optimization。
在利用繼承的實現中,函式物件被包裝在一個子類中,std::function 中持有一個其父類的指標。然而為了效率,我們需要把空白field和這個指標union起來。union總給人一種底層的感覺,在不確定這個union到底儲存的是什麼的時候,當然不能通過其中的指標去呼叫虛擬函式。在這樣的設計中,多型性不再能用繼承體系實現了,我們需要另一種實現多型的方法。
1.3 用函式指標實現多型
回想一下虛擬函式是如何實現的?帶有virtual function的類的物件中會安插vptr,這個指標指向一個vtable,這個vtable含有多個slot,裡面含有指向type_info物件的指標與函式指標——對,我們需要函式指標!不知你有沒有在C中實現過多型,在沒有語言特性的幫助下,比較方便的方法是在struct中直接放函式指標。如果要像C++那樣用上vptr和vtable,你得管理好每個類及其對應vtable的內容。你以為這種情況在C++中就有所好轉嗎?只有你用C++的繼承體系,編譯器才會幫你做這些事。想要自己建立一個從型別到vptr的對映,恐怕你得改編譯器了。
vptr與vtable的意義是什麼?其一,每個基類只對應一個vptr,大小固定,多重繼承下便於管理,但這點與這篇文章的主題沒有關聯;其二,當基類有多個虛擬函式的時候,使用vptr可以節省儲存物件的空間,而如果用函式指標的話,雖然少了一次定址,但繼承帶來的空間overhead取決於虛擬函式的數量,由於至少一個,函式指標的佔用的空間不會少於vptr,在虛擬函式數量較多的情況下,函式指標就要佔用比較大的空間了。
既然我們已經無法在 std::function 中使用vptr,我們也應該儘可能減少函式指標的數量,而這又取決於這些函式的功能,進一步取決於 std::function 類的介面。
1.4 std::function的介面
雖然C++標準規定了 std::function 的介面就應該是這樣,我還是想說說它為什麼應該是這樣。關於其他的一些問題,比如儲存值還是儲存引用等,可以參考相關資料[4]。
最基本的,std::function 是一個模板類,模板引數是一個型別(注意是一個型別,不是好幾個型別)。我們可以這麼寫:
std::function<int(double)> f;
f 是一個可呼叫物件,引數為 double,返回值為 int 。你也許會問,這裡既規定了引數型別又規定了返回值型別,怎麼就成了一個型別呢?確實是一個型別,int(double) 是一個函式型別(注意不是函式指標)。
std::function 要包裝所有合適型別的物件,就必須有對應的建構函式,所以這是個模板建構函式。引數不是通用引用而是直接傳值:
template <typename F> function(F);
可能是為了讓編譯器對空物件進行優化。同樣還有一個模板賦值函式,引數是通用引用。
每個建構函式都有一個添加了 std::allocator_arg_t 作為第一個引數、記憶體分配器物件作為第二個引數的版本,C++17中已經移除(GCC從未提供,可能是因為 std::function 的記憶體分配無法自定義)。同樣刪除的還有 assign ,也是與記憶體分配器相關的。
另外有一個以 std::reference_wrapper 作為引數的賦值函式:
template <typename F> function& operator=(std::reference_wrapper<F>) noexcept;
可以理解為模板賦值函式的特化。沒有相應的建構函式。
預設建構函式、nullptr_t 建構函式、nullptr_t 拷貝賦值函式都將 std::function 物件置空。當 std::function 物件沒有儲存任何函式物件時, operator bool() 返回 false ,與 nullptr_t 呼叫 operator== 會返回 true ,如果呼叫將丟擲 std::bad_function_call 異常。
雖然 std::function 將函式物件包裝了起來,但使用者還是可以獲得原始物件的。target_type() 返回函式物件的 typeid ,target() 模板函式當模板引數與函式物件型別相同時返回其指標,否則返回空指標。
作為函式包裝器,std::function 也是函式物件,可以通過 operator() 呼叫,引數按照模板引數中宣告的型別傳遞。
還有一些介面與大部分STL設施相似,有Rule of Five規定的5個方法、 swap() ,以及 std::swap() 的特化等。可別小看這個 swap() ,它有大用處。
總之,函式物件的複製、移動、賦值、交換等操作都是需要的。對客戶來說,除了兩個 std::function 的相等性判定(筆者最近在嘗試實現這個)以外,其他能想到的方法它都有。
二、std::function的實現
std::function 的實現位於 <functional> ,後續版本遷移至了 <bits/std_function.h> 。下面這段程式碼是GCC 4.8.1(第一個支援完整C++11的版本)中的 <functional> 標頭檔案,共2579行,預設摺疊,慎入。
1 // <functional> -*- C++ -*- 2 3 // Copyright (C) 2001-2013 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /* 26 * Copyright (c) 1997 27 * Silicon Graphics Computer Systems, Inc. 28 * 29 * Permission to use, copy, modify, distribute and sell this software 30 * and its documentation for any purpose is hereby granted without fee, 31 * provided that the above copyright notice appear in all copies and 32 * that both that copyright notice and this permission notice appear 33 * in supporting documentation. Silicon Graphics makes no 34 * representations about the suitability of this software for any 35 * purpose. It is provided "as is" without express or implied warranty. 36 * 37 */ 38 39 /** @file include/functional 40 * This is a Standard C++ Library header. 41 */ 42 43 #ifndef _GLIBCXX_FUNCTIONAL 44 #define _GLIBCXX_FUNCTIONAL 1 45 46 #pragma GCC system_header 47 48 #include <bits/c++config.h> 49 #include <bits/stl_function.h> 50 51 #if __cplusplus >= 201103L 52 53 #include <typeinfo> 54 #include <new> 55 #include <tuple> 56 #include <type_traits> 57 #include <bits/functexcept.h> 58 #include <bits/functional_hash.h> 59 60 namespace std _GLIBCXX_VISIBILITY(default) 61 { 62 _GLIBCXX_BEGIN_NAMESPACE_VERSION 63 64 template<typename _MemberPointer> 65 class _Mem_fn; 66 template<typename _Tp, typename _Class> 67 _Mem_fn<_Tp _Class::*> 68 mem_fn(_Tp _Class::*) noexcept; 69 70 _GLIBCXX_HAS_NESTED_TYPE(result_type) 71 72 /// If we have found a result_type, extract it. 73 template<bool _Has_result_type, typename _Functor> 74 struct _Maybe_get_result_type 75 { }; 76 77 template<typename _Functor> 78 struct _Maybe_get_result_type<true, _Functor> 79 { typedef typename _Functor::result_type result_type; }; 80 81 /** 82 * Base class for any function object that has a weak result type, as 83 * defined in 3.3/3 of TR1. 84 */ 85 template<typename _Functor> 86 struct _Weak_result_type_impl 87 : _Maybe_get_result_type<__has_result_type<_Functor>::value, _Functor> 88 { }; 89 90 /// Retrieve the result type for a function type. 91 template<typename _Res, typename... _ArgTypes> 92 struct _Weak_result_type_impl<_Res(_ArgTypes...)> 93 { typedef _Res result_type; }; 94 95 template<typename _Res, typename... _ArgTypes> 96 struct _Weak_result_type_impl<_Res(_ArgTypes......)> 97 { typedef _Res result_type; }; 98 99 template<typename _Res, typename... _ArgTypes> 100 struct _Weak_result_type_impl<_Res(_ArgTypes...) const> 101 { typedef _Res result_type; }; 102 103 template<typename _Res, typename... _ArgTypes> 104 struct _Weak_result_type_impl<_Res(_ArgTypes......) const> 105 { typedef _Res result_type; }; 106 107 template<typename _Res, typename... _ArgTypes> 108 struct _Weak_result_type_impl<_Res(_ArgTypes...) volatile> 109 { typedef _Res result_type; }; 110 111 template<typename _Res, typename... _ArgTypes> 112 struct _Weak_result_type_impl<_Res(_ArgTypes......) volatile> 113 { typedef _Res result_type; }; 114 115 template<typename _Res, typename... _ArgTypes> 116 struct _Weak_result_type_impl<_Res(_ArgTypes...) const volatile> 117 { typedef _Res result_type; }; 118 119 template<typename _Res, typename... _ArgTypes> 120 struct _Weak_result_type_impl<_Res(_ArgTypes......) const volatile> 121 { typedef _Res result_type; }; 122 123 /// Retrieve the result type for a function reference. 124 template<typename _Res, typename... _ArgTypes> 125 struct _Weak_result_type_impl<_Res(&)(_ArgTypes...)> 126 { typedef _Res result_type; }; 127 128 template<typename _Res, typename... _ArgTypes> 129 struct _Weak_result_type_impl<_Res(&)(_ArgTypes......)> 130 { typedef _Res result_type; }; 131 132 /// Retrieve the result type for a function pointer. 133 template<typename _Res, typename... _ArgTypes> 134 struct _Weak_result_type_impl<_Res(*)(_ArgTypes...)> 135 { typedef _Res result_type; }; 136 137 template<typename _Res, typename... _ArgTypes> 138 struct _Weak_result_type_impl<_Res(*)(_ArgTypes......)> 139 { typedef _Res result_type; }; 140 141 /// Retrieve result type for a member function pointer. 142 template<typename _Res, typename _Class, typename... _ArgTypes> 143 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...)> 144 { typedef _Res result_type; }; 145 146 template<typename _Res, typename _Class, typename... _ArgTypes> 147 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......)> 148 { typedef _Res result_type; }; 149 150 /// Retrieve result type for a const member function pointer. 151 template<typename _Res, typename _Class, typename... _ArgTypes> 152 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) const> 153 { typedef _Res result_type; }; 154 155 template<typename _Res, typename _Class, typename... _ArgTypes> 156 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) const> 157 { typedef _Res result_type; }; 158 159 /// Retrieve result type for a volatile member function pointer. 160 template<typename _Res, typename _Class, typename... _ArgTypes> 161 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) volatile> 162 { typedef _Res result_type; }; 163 164 template<typename _Res, typename _Class, typename... _ArgTypes> 165 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) volatile> 166 { typedef _Res result_type; }; 167 168 /// Retrieve result type for a const volatile member function pointer. 169 template<typename _Res, typename _Class, typename... _ArgTypes> 170 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes...) 171 const volatile> 172 { typedef _Res result_type; }; 173 174 template<typename _Res, typename _Class, typename... _ArgTypes> 175 struct _Weak_result_type_impl<_Res (_Class::*)(_ArgTypes......) 176 const volatile> 177 { typedef _Res result_type; }; 178 179 /** 180 * Strip top-level cv-qualifiers from the function object and let 181 * _Weak_result_type_impl perform the real work. 182 */ 183 template<typename _Functor> 184 struct _Weak_result_type 185 : _Weak_result_type_impl<typename remove_cv<_Functor>::type> 186 { }; 187 188 /// Determines if the type _Tp derives from unary_function. 189 template<typename _Tp> 190 struct _Derives_from_unary_function : __sfinae_types 191 { 192 private: 193 template<typename _T1, typename _Res> 194 static __one __test(const volatile unary_function<_T1, _Res>*); 195 196 // It's tempting to change "..." to const volatile void*, but 197 // that fails when _Tp is a function type. 198 static __two __test(...); 199 200 public: 201 static const bool value = sizeof(__test((_Tp*)0)) == 1; 202 }; 203 204 /// Determines if the type _Tp derives from binary_function. 205 template<typename _Tp> 206 struct _Derives_from_binary_function : __sfinae_types 207 { 208 private: 209 template<typename _T1, typename _T2, typename _Res> 210 static __one __test(const volatile binary_function<_T1, _T2, _Res>*); 211 212 // It's tempting to change "..." to const volatile void*, but 213 // that fails when _Tp is a function type. 214 static __two __test(...); 215 216 public: 217 static const bool value = sizeof(__test((_Tp*)0)) == 1; 218 }; 219 220 /** 221 * Invoke a function object, which may be either a member pointer or a 222 * function object. The first parameter will tell which. 223 */ 224 template<typename _Functor, typename... _Args> 225 inline 226 typename enable_if< 227 (!is_member_pointer<_Functor>::value 228 && !is_function<_Functor>::value 229 && !is_function<typename remove_pointer<_Functor>::type>::value), 230 typename result_of<_Functor&(_Args&&...)>::type 231 >::type 232 __invoke(_Functor& __f, _Args&&... __args) 233 { 234 return __f(std::forward<_Args>(__args)...); 235 } 236 237 template<typename _Functor, typename... _Args> 238 inline 239 typename enable_if< 240 (is_member_pointer<_Functor>::value 241 && !is_function<_Functor>::value 242 && !is_function<typename remove_pointer<_Functor>::type>::value), 243 typename result_of<_Functor(_Args&&...)>::type 244 >::type 245 __invoke(_Functor& __f, _Args&&... __args) 246 { 247 return std::mem_fn(__f)(std::forward<_Args>(__args)...); 248 } 249 250 // To pick up function references (that will become function pointers) 251 template<typename _Functor, typename... _Args> 252 inline 253 typename enable_if< 254 (is_pointer<_Functor>::value 255 && is_function<typename remove_pointer<_Functor>::type>::value), 256 typename result_of<_Functor(_Args&&...)>::type 257 >::type 258 __invoke(_Functor __f, _Args&&... __args) 259 { 260 return __f(std::forward<_Args>(__args)...); 261 } 262 263 /** 264 * Knowing which of unary_function and binary_function _Tp derives 265 * from, derives from the same and ensures that reference_wrapper 266 * will have a weak result type. See cases below. 267 */ 268 template<bool _Unary, bool _Binary, typename _Tp> 269 struct _Reference_wrapper_base_impl; 270 271 // None of the nested argument types. 272 template<typename _Tp> 273 struct _Reference_wrapper_base_impl<false, false, _Tp> 274 : _Weak_result_type<_Tp> 275 { }; 276 277 // Nested argument_type only. 278 template<typename _Tp> 279 struct _Reference_wrapper_base_impl<true, false, _Tp> 280 : _Weak_result_type<_Tp> 281 { 282 typedef typename _Tp::argument_type argument_type; 283 }; 284 285 // Nested first_argument_type and second_argument_type only. 286 template<typename _Tp> 287 struct _Reference_wrapper_base_impl<false, true, _Tp> 288 : _Weak_result_type<_Tp> 289 { 290 typedef typename _Tp::first_argument_type first_argument_type; 291 typedef typename _Tp::second_argument_type second_argument_type; 292 }; 293 294 // All the nested argument types. 295 template<typename _Tp> 296 struct _Reference_wrapper_base_impl<true, true, _Tp> 297 : _Weak_result_type<_Tp> 298 { 299 typedef typename _Tp::argument_type argument_type; 300 typedef typename _Tp::first_argument_type first_argument_type; 301 typedef typename _Tp::second_argument_type second_argument_type; 302 }; 303 304 _GLIBCXX_HAS_NESTED_TYPE(argument_type) 305 _GLIBCXX_HAS_NESTED_TYPE(first_argument_type) 306 _GLIBCXX_HAS_NESTED_TYPE(second_argument_type) 307 308 /** 309 * Derives from unary_function or binary_function when it 310 * can. Specializations handle all of the easy cases. The primary 311 * template determines what to do with a class type, which may 312 * derive from both unary_function and binary_function. 313 */ 314 template<typename _Tp> 315 struct _Reference_wrapper_base 316 : _Reference_wrapper_base_impl< 317 __has_argument_type<_Tp>::value, 318 __has_first_argument_type<_Tp>::value 319 && __has_second_argument_type<_Tp>::value, 320 _Tp> 321 { }; 322 323 // - a function type (unary) 324 template<typename _Res, typename _T1> 325 struct _Reference_wrapper_base<_Res(_T1)> 326 : unary_function<_T1, _Res> 327 { }; 328 329 template<typename _Res, typename _T1> 330 struct _Reference_wrapper_base<_Res(_T1) const> 331 : unary_function<_T1, _Res> 332 { }; 333 334 template<typename _Res, typename _T1> 335 struct _Reference_wrapper_base<_Res(_T1) volatile> 336 : unary_function<_T1, _Res> 337 { }; 338 339 template<typename _Res, typename _T1> 340 struct _Reference_wrapper_base<_Res(_T1) const volatile> 341 : unary_function<_T1, _Res> 342 { }; 343 344 // - a function type (binary) 345 template<typename _Res, typename _T1, typename _T2> 346 struct _Reference_wrapper_base<_Res(_T1, _T2)> 347 : binary_function<_T1, _T2, _Res> 348 { }; 349 350 template<typename _Res, typename _T1, typename _T2> 351 struct _Reference_wrapper_base<_Res(_T1, _T2) const> 352 : binary_function<_T1, _T2, _Res> 353 { }; 354 355 template<typename _Res, typename _T1, typename _T2> 356 struct _Reference_wrapper_base<_Res(_T1, _T2) volatile> 357 : binary_function<_T1, _T2, _Res> 358 { }; 359 360 template<typename _Res, typename _T1, typename _T2> 361 struct _Reference_wrapper_base<_Res(_T1, _T2) const volatile> 362 : binary_function<_T1, _T2, _Res> 363 { }; 364 365 // - a function pointer type (unary) 366 template<typename _Res, typename _T1> 367 struct _Reference_wrapper_base<_Res(*)(_T1)> 368 : unary_function<_T1, _Res> 369 { }; 370 371 // - a function pointer type (binary) 372 template<typename _Res, typename _T1, typename _T2> 373 struct _Reference_wrapper_base<_Res(*)(_T1, _T2)> 374 : binary_function<_T1, _T2, _Res> 375 { }; 376 377 // - a pointer to member function type (unary, no qualifiers) 378 template<typename _Res, typename _T1> 379 struct _Reference_wrapper_base<_Res (_T1::*)()> 380 : unary_function<_T1*, _Res> 381 { }; 382 383 // - a pointer to member function type (binary, no qualifiers) 384 template<typename _Res, typename _T1, typename _T2> 385 struct _Reference_wrapper_base<_Res (_T1::*)(_T2)> 386 : binary_function<_T1*, _T2, _Res> 387 { }; 388 389 // - a pointer to member function type (unary, const) 390 template<typename _Res, typename _T1> 391 struct _Reference_wrapper_base<_Res (_T1::*)() const> 392 : unary_function<const _T1*, _Res> 393 { }; 394 395 // - a pointer to member function type (binary, const) 396 template<typename _Res, typename _T1, typename _T2> 397 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const> 398 : binary_function<const _T1*, _T2, _Res> 399 { }; 400 401 // - a pointer to member function type (unary, volatile) 402 template<typename _Res, typename _T1> 403 struct _Reference_wrapper_base<_Res (_T1::*)() volatile> 404 : unary_function<volatile _T1*, _Res> 405 { }; 406 407 // - a pointer to member function type (binary, volatile) 408 template<typename _Res, typename _T1, typename _T2> 409 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) volatile> 410 : binary_function<volatile _T1*, _T2, _Res> 411 { }; 412 413 // - a pointer to member function type (unary, const volatile) 414 template<typename _Res, typename _T1> 415 struct _Reference_wrapper_base<_Res (_T1::*)() const volatile> 416 : unary_function<const volatile _T1*, _Res> 417 { }; 418 419 // - a pointer to member function type (binary, const volatile) 420 template<typename _Res, typename _T1, typename _T2> 421 struct _Reference_wrapper_base<_Res (_T1::*)(_T2) const volatile> 422 : binary_function<const volatile _T1*, _T2, _Res> 423 { }; 424 425 /** 426 * @brief Primary class template for reference_wrapper. 427 * @ingroup functors 428 * @{ 429 */ 430 template<typename _Tp> 431 class reference_wrapper 432 : public _Reference_wrapper_base<typename remove_cv<_Tp>::type> 433 { 434 _Tp* _M_data; 435 436 public: 437 typedef _Tp type; 438 439 reference_wrapper(_Tp& __indata) noexcept 440 : _M_data(std::__addressof(__indata)) 441 { } 442 443 reference_wrapper(_Tp&&) = delete; 444 445 reference_wrapper(const reference_wrapper<_Tp>& __inref) noexcept 446 : _M_data(__inref._M_data) 447 { } 448 449 reference_wrapper& 450 operator=(const reference_wrapper<_Tp>& __inref) noexcept 451 { 452 _M_data = __inref._M_data; 453 return *this; 454 } 455 456 operator _Tp&() const noexcept 457 { return this->get(); } 458 459 _Tp& 460 get() const noexcept 461 { return *_M_data; } 462 463 template<typename... _Args> 464 typename result_of<_Tp&(_Args&&...)>::type 465 operator()(_Args&&... __args) const 466 { 467 return __invoke(get(), std::forward<_Args>(__args)...); 468 } 469 }; 470 471 472 /// Denotes a reference should be taken to a variable. 473 template<typename _Tp> 474 inline reference_wrapper<_Tp> 475 ref(_Tp& __t) noexcept 476 { return reference_wrapper<_Tp>(__t); } 477 478 /// Denotes a const reference should be taken to a variable. 479 template<typename _Tp> 480 inline reference_wrapper<const _Tp> 481 cref(const _Tp& __t) noexcept 482 { return reference_wrapper<const _Tp>(__t); } 483 484 template<typename _Tp> 485 void ref(const _Tp&&) = delete; 486 487 template<typename _Tp> 488 void cref(const _Tp&&) = delete; 489 490 /// Partial specialization. 491 template<typename _Tp> 492 inline reference_wrapper<_Tp> 493 ref(reference_wrapper<_Tp> __t) noexcept 494 { return ref(__t.get()); } 495 496 /// Partial specialization. 497 template<typename _Tp> 498 inline reference_wrapper<const _Tp> 499 cref(reference_wrapper<_Tp> __t) noexcept 500 { return cref(__t.get()); } 501 502 // @} group functors 503 504 template<typename... _Types> 505 struct _Pack : integral_constant<size_t, sizeof...(_Types)> 506 { }; 507 508 template<typename _From, typename _To, bool = _From::value == _To::value> 509 struct _AllConvertible : false_type 510 { }; 511 512 template<typename... _From, typename... _To> 513 struct _AllConvertible<_Pack<_From...>, _Pack<_To...>, true> 514 : __and_<is_convertible<_From, _To>...> 515 { }; 516 517 template<typename _Tp1, typename _Tp2> 518 using _NotSame = __not_<is_same<typename std::decay<_Tp1>::type, 519 typename std::decay<_Tp2>::type>>; 520 521 /** 522 * Derives from @c unary_function or @c binary_function, or perhaps 523 * nothing, depending on the number of arguments provided. The 524 * primary template is the basis case, which derives nothing. 525 */ 526 template<typename _Res, typename... _ArgTypes> 527 struct _Maybe_unary_or_binary_function { }; 528 529 /// Derives from @c unary_function, as appropriate. 530 template<typename _Res, typename _T1> 531 struct _Maybe_unary_or_binary_function<_Res, _T1> 532 : std::unary_function<_T1, _Res> { }; 533 534 /// Derives from @c binary_function, as appropriate. 535 template<typename _Res, typename _T1, typename _T2> 536 struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> 537 : std::binary_function<_T1, _T2, _Res> { }; 538 539 /// Implementation of @c mem_fn for member function pointers. 540 template<typename _Res, typename _Class, typename... _ArgTypes> 541 class _Mem_fn<_Res (_Class::*)(_ArgTypes...)> 542 : public _Maybe_unary_or_binary_function<_Res, _Class*, _ArgTypes...> 543 { 544 typedef _Res (_Class::*_Functor)(_ArgTypes...); 545 546 template<typename _Tp, typename... _Args> 547 _Res 548 _M_call(_Tp&& __object, const volatile _Class *, 549 _Args&&... __args) const 550 { 551 return (std::forward<_Tp>(__object).*__pmf) 552 (std::forward<_Args>(__args)...); 553 } 554 555 template<typename _Tp, typename... _Args> 556 _Res 557 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const 558 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); } 559 560 // Require each _Args to be convertible to corresponding _ArgTypes 561 template<typename... _Args> 562 using _RequireValidArgs 563 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 564 565 // Require each _Args to be convertible to corresponding _ArgTypes 566 // and require _Tp is not _Class, _Class& or _Class* 567 template<typename _Tp, typename... _Args> 568 using _RequireValidArgs2 569 = _Require<_NotSame<_Class, _Tp>, _NotSame<_Class*, _Tp>, 570 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 571 572 // Require each _Args to be convertible to corresponding _ArgTypes 573 // and require _Tp is _Class or derived from _Class 574 template<typename _Tp, typename... _Args> 575 using _RequireValidArgs3 576 = _Require<is_base_of<_Class, _Tp>, 577 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 578 579 public: 580 typedef _Res result_type; 581 582 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 583 584 // Handle objects 585 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 586 _Res 587 operator()(_Class& __object, _Args&&... __args) const 588 { return (__object.*__pmf)(std::forward<_Args>(__args)...); } 589 590 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 591 _Res 592 operator()(_Class&& __object, _Args&&... __args) const 593 { 594 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...); 595 } 596 597 // Handle pointers 598 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 599 _Res 600 operator()(_Class* __object, _Args&&... __args) const 601 { return (__object->*__pmf)(std::forward<_Args>(__args)...); } 602 603 // Handle smart pointers, references and pointers to derived 604 template<typename _Tp, typename... _Args, 605 typename _Req = _RequireValidArgs2<_Tp, _Args...>> 606 _Res 607 operator()(_Tp&& __object, _Args&&... __args) const 608 { 609 return _M_call(std::forward<_Tp>(__object), &__object, 610 std::forward<_Args>(__args)...); 611 } 612 613 template<typename _Tp, typename... _Args, 614 typename _Req = _RequireValidArgs3<_Tp, _Args...>> 615 _Res 616 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const 617 { return operator()(__ref.get(), std::forward<_Args>(__args)...); } 618 619 private: 620 _Functor __pmf; 621 }; 622 623 /// Implementation of @c mem_fn for const member function pointers. 624 template<typename _Res, typename _Class, typename... _ArgTypes> 625 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const> 626 : public _Maybe_unary_or_binary_function<_Res, const _Class*, 627 _ArgTypes...> 628 { 629 typedef _Res (_Class::*_Functor)(_ArgTypes...) const; 630 631 template<typename _Tp, typename... _Args> 632 _Res 633 _M_call(_Tp&& __object, const volatile _Class *, 634 _Args&&... __args) const 635 { 636 return (std::forward<_Tp>(__object).*__pmf) 637 (std::forward<_Args>(__args)...); 638 } 639 640 template<typename _Tp, typename... _Args> 641 _Res 642 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const 643 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); } 644 645 template<typename... _Args> 646 using _RequireValidArgs 647 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 648 649 template<typename _Tp, typename... _Args> 650 using _RequireValidArgs2 651 = _Require<_NotSame<_Class, _Tp>, _NotSame<const _Class*, _Tp>, 652 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 653 654 template<typename _Tp, typename... _Args> 655 using _RequireValidArgs3 656 = _Require<is_base_of<_Class, _Tp>, 657 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 658 659 public: 660 typedef _Res result_type; 661 662 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 663 664 // Handle objects 665 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 666 _Res 667 operator()(const _Class& __object, _Args&&... __args) const 668 { return (__object.*__pmf)(std::forward<_Args>(__args)...); } 669 670 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 671 _Res 672 operator()(const _Class&& __object, _Args&&... __args) const 673 { 674 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...); 675 } 676 677 // Handle pointers 678 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 679 _Res 680 operator()(const _Class* __object, _Args&&... __args) const 681 { return (__object->*__pmf)(std::forward<_Args>(__args)...); } 682 683 // Handle smart pointers, references and pointers to derived 684 template<typename _Tp, typename... _Args, 685 typename _Req = _RequireValidArgs2<_Tp, _Args...>> 686 _Res operator()(_Tp&& __object, _Args&&... __args) const 687 { 688 return _M_call(std::forward<_Tp>(__object), &__object, 689 std::forward<_Args>(__args)...); 690 } 691 692 template<typename _Tp, typename... _Args, 693 typename _Req = _RequireValidArgs3<_Tp, _Args...>> 694 _Res 695 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const 696 { return operator()(__ref.get(), std::forward<_Args>(__args)...); } 697 698 private: 699 _Functor __pmf; 700 }; 701 702 /// Implementation of @c mem_fn for volatile member function pointers. 703 template<typename _Res, typename _Class, typename... _ArgTypes> 704 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) volatile> 705 : public _Maybe_unary_or_binary_function<_Res, volatile _Class*, 706 _ArgTypes...> 707 { 708 typedef _Res (_Class::*_Functor)(_ArgTypes...) volatile; 709 710 template<typename _Tp, typename... _Args> 711 _Res 712 _M_call(_Tp&& __object, const volatile _Class *, 713 _Args&&... __args) const 714 { 715 return (std::forward<_Tp>(__object).*__pmf) 716 (std::forward<_Args>(__args)...); 717 } 718 719 template<typename _Tp, typename... _Args> 720 _Res 721 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const 722 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); } 723 724 template<typename... _Args> 725 using _RequireValidArgs 726 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 727 728 template<typename _Tp, typename... _Args> 729 using _RequireValidArgs2 730 = _Require<_NotSame<_Class, _Tp>, _NotSame<volatile _Class*, _Tp>, 731 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 732 733 template<typename _Tp, typename... _Args> 734 using _RequireValidArgs3 735 = _Require<is_base_of<_Class, _Tp>, 736 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 737 738 public: 739 typedef _Res result_type; 740 741 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 742 743 // Handle objects 744 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 745 _Res 746 operator()(volatile _Class& __object, _Args&&... __args) const 747 { return (__object.*__pmf)(std::forward<_Args>(__args)...); } 748 749 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 750 _Res 751 operator()(volatile _Class&& __object, _Args&&... __args) const 752 { 753 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...); 754 } 755 756 // Handle pointers 757 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 758 _Res 759 operator()(volatile _Class* __object, _Args&&... __args) const 760 { return (__object->*__pmf)(std::forward<_Args>(__args)...); } 761 762 // Handle smart pointers, references and pointers to derived 763 template<typename _Tp, typename... _Args, 764 typename _Req = _RequireValidArgs2<_Tp, _Args...>> 765 _Res 766 operator()(_Tp&& __object, _Args&&... __args) const 767 { 768 return _M_call(std::forward<_Tp>(__object), &__object, 769 std::forward<_Args>(__args)...); 770 } 771 772 template<typename _Tp, typename... _Args, 773 typename _Req = _RequireValidArgs3<_Tp, _Args...>> 774 _Res 775 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const 776 { return operator()(__ref.get(), std::forward<_Args>(__args)...); } 777 778 private: 779 _Functor __pmf; 780 }; 781 782 /// Implementation of @c mem_fn for const volatile member function pointers. 783 template<typename _Res, typename _Class, typename... _ArgTypes> 784 class _Mem_fn<_Res (_Class::*)(_ArgTypes...) const volatile> 785 : public _Maybe_unary_or_binary_function<_Res, const volatile _Class*, 786 _ArgTypes...> 787 { 788 typedef _Res (_Class::*_Functor)(_ArgTypes...) const volatile; 789 790 template<typename _Tp, typename... _Args> 791 _Res 792 _M_call(_Tp&& __object, const volatile _Class *, 793 _Args&&... __args) const 794 { 795 return (std::forward<_Tp>(__object).*__pmf) 796 (std::forward<_Args>(__args)...); 797 } 798 799 template<typename _Tp, typename... _Args> 800 _Res 801 _M_call(_Tp&& __ptr, const volatile void *, _Args&&... __args) const 802 { return ((*__ptr).*__pmf)(std::forward<_Args>(__args)...); } 803 804 template<typename... _Args> 805 using _RequireValidArgs 806 = _Require<_AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 807 808 template<typename _Tp, typename... _Args> 809 using _RequireValidArgs2 810 = _Require<_NotSame<_Class, _Tp>, 811 _NotSame<const volatile _Class*, _Tp>, 812 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 813 814 template<typename _Tp, typename... _Args> 815 using _RequireValidArgs3 816 = _Require<is_base_of<_Class, _Tp>, 817 _AllConvertible<_Pack<_Args...>, _Pack<_ArgTypes...>>>; 818 819 public: 820 typedef _Res result_type; 821 822 explicit _Mem_fn(_Functor __pmf) : __pmf(__pmf) { } 823 824 // Handle objects 825 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 826 _Res 827 operator()(const volatile _Class& __object, _Args&&... __args) const 828 { return (__object.*__pmf)(std::forward<_Args>(__args)...); } 829 830 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 831 _Res 832 operator()(const volatile _Class&& __object, _Args&&... __args) const 833 { 834 return (std::move(__object).*__pmf)(std::forward<_Args>(__args)...); 835 } 836 837 // Handle pointers 838 template<typename... _Args, typename _Req = _RequireValidArgs<_Args...>> 839 _Res 840 operator()(const volatile _Class* __object, _Args&&... __args) const 841 { return (__object->*__pmf)(std::forward<_Args>(__args)...); } 842 843 // Handle smart pointers, references and pointers to derived 844 template<typename _Tp, typename... _Args, 845 typename _Req = _RequireValidArgs2<_Tp, _Args...>> 846 _Res operator()(_Tp&& __object, _Args&&... __args) const 847 { 848 return _M_call(std::forward<_Tp>(__object), &__object, 849 std::forward<_Args>(__args)...); 850 } 851 852 template<typename _Tp, typename... _Args, 853 typename _Req = _RequireValidArgs3<_Tp, _Args...>> 854 _Res 855 operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const 856 { return operator()(__ref.get(), std::forward<_Args>(__args)...); } 857 858 private: 859 _Functor __pmf; 860 }; 861 862 863 template<typename _Tp, bool> 864 struct _Mem_fn_const_or_non 865 { 866 typedef const _Tp& type; 867 }; 868 869 template<typename _Tp> 870 struct _Mem_fn_const_or_non<_Tp, false> 871 { 872 typedef _Tp& type; 873 }; 874 875 template<typename _Res, typename _Class> 876 class _Mem_fn<_Res _Class::*> 877 { 878 using __pm_type = _Res _Class::*; 879 880 // This bit of genius is due to Peter Dimov, improved slightly by 881 // Douglas Gregor. 882 // Made less elegant to support perfect forwarding and noexcept. 883 template<typename _Tp> 884 auto 885 _M_call(_Tp&& __object, const _Class *) const noexcept 886 -> decltype(std::forward<_Tp>(__object).*std::declval<__pm_type&>()) 887 { return std::forward<_Tp>(__object).*__pm; } 888 889 template<typename _Tp, typename _Up> 890 auto 891 _M_call(_Tp&& __object, _Up * const *) const noexcept 892 -> decltype((*std::forward<_Tp>(__object)).*std::declval<__pm_type&>()) 893 { return (*std::forward<_Tp>(__object)).*__pm; } 894 895 template<typename _Tp> 896 auto 897 _M_call(_Tp&& __ptr, const volatile void*) const 898 noexcept(noexcept((*__ptr).*std::declval<__pm_type&>())) 899 -> decltype((*__ptr).*std::declval<__pm_type&>()) 900 { return (*__ptr).*__pm; } 901 902 public: 903 explicit 904 _Mem_fn(_Res _Class::*__pm) noexcept : __pm(__pm) { } 905 906 // Handle objects 907 _Res& 908 operator()(_Class& __object) const noexcept 909 { return __object.*__pm; } 910 911 const _Res& 912 operator()(const _Class& __object) const noexcept 913 { return __object.*__pm; } 914 915 _Res&& 916 operator()(_Class&& __object) const noexcept 917 { return std::forward<_Class>(__object).*__pm; } 918 919 const _Res&& 920 operator()(const _Class&& __object) const noexcept 921 { return std::forward<const _Class>(__object).*__pm; } 922 923 // Handle pointers 924 _Res& 925 operator()(_Class* __object) const noexcept 926 { return __object->*__pm; } 927 928 const _Res& 929 operator()(const _Class* __object) const noexcept 930 { return __object->*__pm; } 931 932 // Handle smart pointers and derived 933 template<typename _Tp, typename _Req = _Require<_NotSame<_Class*, _Tp>>> 934 auto 935 operator()(_Tp&& __unknown) const 936 noexcept(noexcept(std::declval<_Mem_fn*>()->_M_call 937 (std::forward<_Tp>(__unknown), &__unknown))) 938 -> decltype(this->_M_call(std::forward<_Tp>(__unknown), &__unknown)) 939 { return _M_call(std::forward<_Tp>(__unknown), &__unknown); } 940 941 template<typename _Tp, typename _Req = _Require<is_base_of<_Class, _Tp>>> 942 auto 943 operator()(reference_wrapper<_Tp> __ref) const 944 noexcept(noexcept(std::declval<_Mem_fn&>()(__ref.get()))) 945 -> decltype((*this)(__ref.get())) 946 { return (*this)(__ref.get()); } 947 948 private: 949 _Res _Class::*__pm; 950 }; 951 952 // _GLIBCXX_RESOLVE_LIB_DEFECTS 953 // 2048. Unnecessary mem_fn overloads 954 /** 955 * @brief Returns a function object that forwards to the member 956 * pointer @a pm. 957 * @ingroup functors 958 */ 959 template<typename _Tp, typename _Class> 960 inline _Mem_fn<_Tp _Class::*> 961 mem_fn(_Tp _Class::* __pm) noexcept 962 { 963 return _Mem_fn<_Tp _Class::*>(__pm); 964 } 965 966 /** 967 * @brief Determines if the given type _Tp is a function object 968 * should be treated as a subexpression when evaluating calls to 969 * function objects returned by bind(). [TR1 3.6.1] 970 * @ingroup binders 971 */ 972 template<typename _Tp> 973 struct is_bind_expression 974 : public false_type { }; 975 976 /** 977 * @brief Determines if the given type _Tp is a placeholder in a 978 * bind() expression and, if so, which placeholder it is. [TR1 3.6.2] 979 * @ingroup binders 980 */ 981 template<typename _Tp> 982 struct is_placeholder 983 : public integral_constant<int, 0> 984 { }; 985 986 /** @brief The type of placeholder objects defined by libstdc++. 987 * @ingroup binders 988 */ 989 template<int _Num> struct _Placeholder { }; 990 991 _GLIBCXX_END_NAMESPACE_VERSION 992 993 /** @namespace std::placeholders 994 * @brief ISO C++11 entities sub-namespace for functional. 995 * @ingroup binders 996 */ 997 namespace placeholders 998 { 999 _GLIBCXX_BEGIN_NAMESPACE_VERSION 1000 /* Define a large number of placeholders. There is no way to 1001 * simplify this with variadic templates, because we're introducing 1002 * unique names for each. 1003 */ 1004 extern const _Placeholder<1> _1; 1005 extern const _Placeholder<2> _2; 1006 extern const _Placeholder<3> _3; 1007 extern const _Placeholder<4> _4; 1008 extern const _Placeholder<5> _5; 1009 extern const _Placeholder<6> _6; 1010 extern const _Placeholder<7> _7; 1011 extern const _Placeholder<8> _8; 1012 extern const _Placeholder<9> _9; 1013 extern const _Placeholder<10> _10; 1014 extern const _Placeholder<11> _11; 1015 extern const _Placeholder<12> _12; 1016 extern const _Placeholder<13> _13; 1017 extern const _Placeholder<14> _14; 1018 extern const _Placeholder<15> _15; 1019 extern const _Placeholder<16> _16; 1020 extern const _Placeholder<17> _17; 1021 extern const _Placeholder<18> _18; 1022 extern const _Placeholder<19> _19; 1023 extern const _Placeholder<20> _20; 1024 extern const _Placeholder<21> _21; 1025 extern const _Placeholder<22> _22; 1026 extern const _Placeholder<23> _23; 1027 extern const _Placeholder<24> _24; 1028 extern const _Placeholder<25> _25; 1029 extern const _Placeholder<26> _26; 1030 extern const _Placeholder<27> _27; 1031 extern const _Placeholder<28> _28; 1032 extern const _Placeholder<29> _29; 1033 _GLIBCXX_END_NAMESPACE_VERSION 1034 } 1035 1036 _GLIBCXX_BEGIN_NAMESPACE_VERSION 1037 1038 /** 1039 * Partial specialization of is_placeholder that provides the placeholder 1040 * number for the placeholder objects defined by libstdc++. 1041 * @ingroup binders 1042 */ 1043 template<int _Num> 1044 struct is_placeholder<_Placeholder<_Num> > 1045 : public integral_constant<int, _Num> 1046 { }; 1047 1048 template<int _Num> 1049 struct is_placeholder<const _Placeholder<_Num> > 1050 : public integral_constant<int, _Num> 1051 { }; 1052 1053 /** 1054 * Used by _Safe_tuple_element to indicate that there is no tuple 1055 * element at this position. 1056 */ 1057 struct _No_tuple_element; 1058 1059 /** 1060 * Implementation helper for _Safe_tuple_element. This primary 1061 * template handles the case where it is safe to use @c 1062 * tuple_element. 1063 */ 1064 template<std::size_t __i, typename _Tuple, bool _IsSafe> 1065 struct _Safe_tuple_element_impl 1066 : tuple_element<__i, _Tuple> { }; 1067 1068 /** 1069 * Implementation helper for _Safe_tuple_element. This partial 1070 * specialization handles the case where it is not safe to use @c 1071 * tuple_element. We just return @c _No_tuple_element. 1072 */ 1073 template<std::size_t __i, typename _Tuple> 1074 struct _Safe_tuple_element_impl<__i, _Tuple, false> 1075 { 1076 typedef _No_tuple_element type; 1077 }; 1078 1079 /** 1080 * Like tuple_element, but returns @c _No_tuple_element when 1081 * tuple_element would return an error. 1082 */ 1083 template<std::size_t __i, typename _Tuple> 1084 struct _Safe_tuple_element 1085 : _Safe_tuple_element_impl<__i, _Tuple, 1086 (__i < tuple_size<_Tuple>::value)> 1087 { }; 1088 1089 /** 1090 * Maps an argument to bind() into an actual argument to the bound 1091 * function object [TR1 3.6.3/5]. Only the first parameter should 1092 * be specified: the rest are used to determine among the various 1093 * implementations. Note that, although this class is a function 1094 * object, it isn't entirely normal because it takes only two 1095 * parameters regardless of the number of parameters passed to the 1096 * bind expression. The first parameter is the bound argument and 1097 * the second parameter is a tuple containing references to the 1098 * rest of the arguments. 1099 */ 1100 template<typename _Arg, 1101 bool _IsBindExp = is_bind_expression<_Arg>::value, 1102 bool _IsPlaceholder = (is_placeholder<_Arg>::value > 0)> 1103 class _Mu; 1104 1105 /** 1106 * If the argument is reference_wrapper<_Tp>, returns the 1107 * underlying reference. [TR1 3.6.3/5 bullet 1] 1108 */ 1109 template<typename _Tp> 1110 class _Mu<reference_wrapper<_Tp>, false, false> 1111 { 1112 public: 1113 typedef _Tp& result_type; 1114 1115 /* Note: This won't actually work for const volatile 1116 * reference_wrappers, because reference_wrapper::get() is const 1117 * but not volatile-qualified. This might be a defect in the TR. 1118 */ 1119 template<typename _CVRef, typename _Tuple> 1120 result_type 1121 operator()(_CVRef& __arg, _Tuple&) const volatile 1122 { return __arg.get(); } 1123 }; 1124 1125 /** 1126 * If the argument is a bind expression, we invoke the underlying 1127 * function object with the same cv-qualifiers as we are given and 1128 * pass along all of our arguments (unwrapped). [TR1 3.6.3/5 bullet 2] 1129 */ 1130 template<typename _Arg> 1131 class _Mu<_Arg, true, false> 1132 { 1133 public: 1134 template<typename _CVArg, typename... _Args> 1135 auto 1136 operator()(_CVArg& __arg, 1137 tuple<_Args...>& __tuple) const volatile 1138 -> decltype(__arg(declval<_Args>()...)) 1139 { 1140 // Construct an index tuple and forward to __call 1141 typedef typename _Build_index_tuple<sizeof...(_Args)>::__type 1142 _Indexes; 1143 return this->__call(__arg, __tuple, _Indexes()); 1144 } 1145 1146 private: 1147 // Invokes the underlying function object __arg by unpacking all 1148 // of the arguments in the tuple. 1149 template<typename _CVArg, typename... _Args, std::size_t... _Indexes> 1150 auto 1151 __call(_CVArg& __arg, tuple<_Args...>& __tuple, 1152 const _Index_tuple<_Indexes...>&) const volatile 1153 -> decltype(__arg(declval<_Args>()...)) 1154 { 1155 return __arg(std::forward<_Args>(get<_Indexes>(__tuple))...); 1156 } 1157 }; 1158 1159 /** 1160 * If the argument is a placeholder for the Nth argument, returns 1161 * a reference to the Nth argument to the bind function object. 1162 * [TR1 3.6.3/5 bullet 3] 1163 */ 1164 template<typename _Arg> 1165 class _Mu<_Arg, false, true> 1166 { 1167 public: 1168 template<typename _Signature> class result; 1169 1170 template<typename _CVMu, typename _CVArg, typename _Tuple> 1171 class result<_CVMu(_CVArg, _Tuple)> 1172 { 1173 // Add a reference, if it hasn't already been done for us. 1174 // This allows us to be a little bit sloppy in constructing 1175 // the tuple that we pass to result_of<...>. 1176 typedef typename _Safe_tuple_element<(is_placeholder<_Arg>::value 1177 - 1), _Tuple>::type 1178 __base_type; 1179 1180 public: 1181 typedef typename add_rvalue_reference<__base_type>::type type; 1182 }; 1183 1184 template<typename _Tuple> 1185 typename result<_Mu(_Arg, _Tuple)>::type 1186 operator()(const volatile _Arg&, _Tuple& __tuple) const volatile 1187 { 1188 return std::forward<typename result<_Mu(_Arg, _Tuple)>::type>( 1189 ::std::get<(is_placeholder<_Arg>::value - 1)>(__tuple)); 1190 } 1191 }; 1192 1193 /** 1194 * If the argument is just a value, returns a reference to that 1195 * value. The cv-qualifiers on the reference are the same as the 1196 * cv-qualifiers on the _Mu object. [TR1 3.6.3/5 bullet 4] 1197 */ 1198 template<typename _Arg> 1199 class _Mu<_Arg, false, false> 1200 { 1201 public: 1202 template<typename _Signature> struct result; 1203 1204 template<typename _CVMu, typename _CVArg, typename _Tuple> 1205 struct result<_CVMu(_CVArg, _Tuple)> 1206 { 1207 typedef typename add_lvalue_reference<_CVArg>::type type; 1208 }; 1209 1210 // Pick up the cv-qualifiers of the argument 1211 template<typename _CVArg, typename _Tuple> 1212 _CVArg&& 1213 operator()(_CVArg&& __arg, _Tuple&) const volatile 1214 { return std::forward<_CVArg>(__arg); } 1215 }; 1216 1217 /** 1218 * Maps member pointers into instances of _Mem_fn but leaves all 1219 * other function objects untouched. Used by tr1::bind(). The 1220 * primary template handles the non--member-pointer case. 1221 */ 1222 template<typename _Tp> 1223 struct _Maybe_wrap_member_pointer 1224 { 1225 typedef _Tp type; 1226 1227 static const _Tp& 1228 __do_wrap(const _Tp& __x) 1229 { return __x; } 1230 1231 static _Tp&& 1232 __do_wrap(_Tp&& __x) 1233 { return static_cast<_Tp&&>(__x); } 1234 }; 1235 1236 /** 1237 * Maps member pointers into instances of _Mem_fn but leaves all 1238 * other function objects untouched. Used by tr1::bind(). This 1239 * partial specialization handles the member pointer case. 1240 */ 1241 template<typename _Tp, typename _Class> 1242 struct _Maybe_wrap_member_pointer<_Tp _Class::*> 1243 { 1244 typedef _Mem_fn<_Tp _Class::*> type; 1245 1246 static type 1247 __do_wrap(_Tp _Class::* __pm) 1248 { return type(__pm); } 1249 }; 1250 1251 // Specialization needed to prevent "forming reference to void" errors when 1252 // bind<void>() is called, because argument deduction instantiates 1253 // _Maybe_wrap_member_pointer<void> outside the immediate context where 1254 // SFINAE applies. 1255 template<> 1256 struct _Maybe_wrap_member_pointer<void> 1257 { 1258 typedef void type; 1259 }; 1260 1261 // std::get<I> for volatile-qualified tuples 1262 template<std::size_t _Ind, typename... _Tp> 1263 inline auto 1264 __volget(volatile tuple<_Tp...>& __tuple) 1265 -> typename tuple_element<_Ind, tuple<_Tp...>>::type volatile& 1266 { return std::get<_Ind>(const_cast<tuple<_Tp...>&>(__tuple)); } 1267 1268 // std::get<I&