可呼叫物件的包裝函式。
語法
template <class Fty>
class function // Fty of type Ret(T1, T2, ..., TN)
: public unary_function<T1, Ret> // when Fty is Ret(T1)
: public binary_function<T1, T2, Ret> // when Fty is Ret(T1, T2)
{
public:
typedef Ret result_type;
function();
function(nullptr_t);
function(const function& right);
template <class Fty2>
function(Fty2 fn);
template <class Fty2, class Alloc>
function(reference_wrapper<Fty2>, const Alloc& Ax);
template <class Fty2, class Alloc>
void assign(Fty2, const Alloc& Ax);
template <class Fty2, class Alloc>
void assign(reference_wrapper<Fty2>, const Alloc& Ax);
function& operator=(nullptr_t);
function& operator=(const function&);
template <class Fty2>
function& operator=(Fty2);
template <class Fty2>
function& operator=(reference_wrapper<Fty2>);
void swap(function&);
explicit operator bool() const;
result_type operator()(T1, T2, ....., TN) const;
const std::type_info& target_type() const;
template <class Fty2>
Fty2 *target();
template <class Fty2>
const Fty2 *target() const;
template <class Fty2>
void operator==(const Fty2&) const = delete;
template <class Fty2>
void operator!=(const Fty2&) const = delete;
};
參數
Fty
要包裝的函式類型。
斧頭
配置器函數。
備註
類別範本是呼叫簽章為 Ret(T1, T2, ..., TN)的呼叫包裝函式。 您可以使用它,將各種可呼叫的物件括在統一包裝函式中。
有些成員函式會採用運算元來命名所需的目標物件。 您可以透過數種方法指定這類運算元:
fn:可呼叫的物件 fn;在呼叫之後, function 物件會保存 的複本。 fn
fnref:由 所命名 fnref.get()的可呼叫物件;在呼叫之後, function 物件會保存 的參考 fnref.get()
right:可呼叫的物件,如果有的話,由 function 物件保留 right
npc:Null 指標;呼叫 function 對象之後是空的
在所有情況下, INVOKE(f, t1, t2, ..., tN)其中 f 是可呼叫的物件,而且 t1, t2, ..., tN 是型 T1, T2, ..., TN 別的左值,必須格式良好,如果 Ret 不是 void,可轉換成 Ret。
空 function 物件不會保存可呼叫的物件或可呼叫對象的參考。
成員
建構函式
| 名稱 | 描述 |
|---|---|
| 函數 | 可建構空的包裝函式,或儲存含固定簽章的任意類型可呼叫物件。 |
Typedefs
| 名稱 | 描述 |
|---|---|
| result_type | 為預存的可呼叫物件的傳回類型。 |
函式
| 名稱 | 描述 |
|---|---|
| assign | 會將可呼叫物件指派給此函式物件。 |
| swap | 交換兩個可呼叫物件。 |
| 目標 | 測試預存的可呼叫物件是否如指定般為可呼叫。 |
| target_type | 取得可呼叫物件的類型資訊。 |
操作員
| 名稱 | 描述 |
|---|---|
| 運算子未指定 | 測試預存的可呼叫物件是否存在。 |
| operator() | 呼叫可呼叫物件。 |
| operator= | 取代預存的可呼叫物件。 |
assign
會將可呼叫物件指派給此函式物件。
template <class Fx, class Alloc>
void assign(
Fx _Func,
const Alloc& Ax);
template <class Fx, class Alloc>
void assign(
reference_wrapper<Fx> _Fnref,
const Alloc& Ax);
參數
_Func
可呼叫的物件。
_Fnref
參考包裝函式,其中包含可呼叫物件。
斧頭
配置器物件。
備註
每個成員函式皆會將 *this 所保留的 callable object 取代為以 operand 傳遞的可呼叫物件。 這兩者都會配置記憶體與配置器物件 Ax。
函數
可建構空的包裝函式,或儲存含固定簽章的任意類型可呼叫物件。
function();
function(nullptr_t npc);
function(const function& right);
template <class Fx>
function(Fx _Func);
template <class Fx>
function(reference_wrapper<Fx> _Fnref);
template <class Fx, class Alloc>
function(
Fx _Func,
const Alloc& Ax);
template <class Fx, class Alloc>
function(
reference_wrapper<Fx> _Fnref,
const Alloc& Ax);
參數
right
要複製的函式物件。
外匯
可呼叫物件的類型。
_Func
要包裝的可呼叫物件。
Alloc
配置器類型。
斧頭
配置器。
_Fnref
要包裝的可呼叫物件參考。
備註
前兩個建構函式會建構空的 function 物件。 接下來的三個建構函式會建構 function 物件,其中保留以運算元傳遞的可呼叫物件。 最後兩個建構函式會使用配置器物件 Ax 來配置儲存體。
範例
// std__functional__function_function.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>
#include <vector>
int square(int val)
{
return val * val;
}
class multiply_by
{
public:
explicit multiply_by(const int n) : m_n(n) { }
int operator()(const int x) const
{
return m_n * x;
}
private:
int m_n;
};
int main()
{
typedef std::vector< std::function<int (int)> > vf_t;
vf_t v;
v.push_back(square);
v.push_back(std::negate<int>());
v.push_back(multiply_by(3));
for (vf_t::const_iterator i = v.begin(); i != v.end(); ++i)
{
std::cout << (*i)(10) << std::endl;
}
std::function<int (int)> f = v[0];
std::function<int (int)> g;
if (f) {
std::cout << "f is non-empty (correct)." << std::endl;
} else {
std::cout << "f is empty (can't happen)." << std::endl;
}
if (g) {
std::cout << "g is non-empty (can't happen)." << std::endl;
} else {
std::cout << "g is empty (correct)." << std::endl;
}
return 0;
}
100
-10
30
f is non-empty (correct).
g is empty (correct).
運算子未指定
測試預存的可呼叫物件是否存在。
operator unspecified();
備註
只有當物件不是空的時,運算子才會傳回可轉換為 bool true 值的值。 您可以使用它來測試物件是否是空的。
範例
// std__functional__function_operator_bool.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>
int neg(int val)
{
return (-val);
}
int main()
{
std::function<int (int)> fn0;
std::cout << std::boolalpha << "not empty == " << (bool)fn0 << std::endl;
std::function<int (int)> fn1(neg);
std::cout << std::boolalpha << "not empty == " << (bool)fn1 << std::endl;
return (0);
}
not empty == false
not empty == true
operator()
呼叫可呼叫物件。
result_type operator()(
T1 t1,
T2 t2, ...,
TN tN);
參數
TN
第 N 個呼叫引數類型。
tN
第 N 個呼叫引數。
備註
成員函式會傳回 INVOKE(fn, t1, t2, ..., tN, Ret),其中 fn 是儲存在 *this 中的目標物件。 您可以使用此項目來呼叫已包裝的可呼叫物件。
範例
// std__functional__function_operator_call.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>
int neg(int val)
{
return (-val);
}
int main()
{
std::function<int (int)> fn1(neg);
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
std::cout << "val == " << fn1(3) << std::endl;
return (0);
}
empty == false
val == -3
operator=
取代預存的可呼叫物件。
function& operator=(null_ptr_type npc);
function& operator=(const function& right);
template <class Fty>
function& operator=(Fty fn);
template <class Fty>
function& operator=(reference_wrapper<Fty> fnref);
參數
全國人大
null 指標常數。
right
要複製的函式物件。
fn
要包裝的可呼叫物件。
fnref
要包裝的可呼叫物件參考。
備註
每個運算子皆會將 *this 所保留的可呼叫物件取代為以運算元傳遞的可呼叫物件。
範例
// std__functional__function_operator_as.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>
int neg(int val)
{
return (-val);
}
int main()
{
std::function<int (int)> fn0(neg);
std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
std::cout << "val == " << fn0(3) << std::endl;
std::function<int (int)> fn1;
fn1 = 0;
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
fn1 = neg;
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
std::cout << "val == " << fn1(3) << std::endl;
fn1 = fn0;
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
std::cout << "val == " << fn1(3) << std::endl;
fn1 = std::cref(fn1);
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
std::cout << "val == " << fn1(3) << std::endl;
return (0);
}
empty == false
val == -3
empty == true
empty == false
val == -3
empty == false
val == -3
empty == false
val == -3
result_type
為預存的可呼叫物件的傳回類型。
typedef Ret result_type;
備註
typedef 與範本呼叫簽章中的 Ret 類型同義。 您可以使用它來判斷所包裝之可呼叫物件的傳回類型。
範例
// std__functional__function_result_type.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>
int neg(int val)
{
return (-val);
}
int main()
{
std::function<int (int)> fn1(neg);
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
std::function<int (int)>::result_type val = fn1(3);
std::cout << "val == " << val << std::endl;
return (0);
}
empty == false
val == -3
swap
交換兩個可呼叫物件。
void swap(function& right);
參數
right
要交換的函式物件。
備註
成員函式會在和 右側之間*this交換目標物件。 它會在固定時間執行但不會擲回任何例外狀況。
範例
// std__functional__function_swap.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>
int neg(int val)
{
return (-val);
}
int main()
{
std::function<int (int)> fn0(neg);
std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
std::cout << "val == " << fn0(3) << std::endl;
std::function<int (int)> fn1;
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
std::cout << std::endl;
fn0.swap(fn1);
std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
std::cout << "val == " << fn1(3) << std::endl;
return (0);
}
empty == false
val == -3
empty == true
empty == true
empty == false
val == -3
目標
測試預存的可呼叫物件是否如指定般為可呼叫。
template <class Fty2>
Fty2 *target();
template <class Fty2>
const Fty2 *target() const;
參數
Fty2
要測試的目標可呼叫物件類型。
備註
Fty2 類型必須是自變數型T1, T2, ..., TN別和傳回型別的可呼叫。Ret 如果 target_type() == typeid(Fty2),成員範本函式會傳回目標物件的位址;否則會傳回 0。
Fty2 類型可呼叫自變數類型和T1, T2, ..., TN傳回型Ret別,如果分別針對 類型的 Fty2, T1, T2, ..., TNlvaluefn, t1, t2, ..., tN,則為格式正確的 ,如果 Ret 不是 void,INVOKE(fn, t1, t2, ..., tN)則可轉換成 Ret。
範例
// std__functional__function_target.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>
int neg(int val)
{
return (-val);
}
int main()
{
typedef int (*Myfun)(int);
std::function<int (int)> fn0(neg);
std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
std::cout << "no target == " << (fn0.target<Myfun>() == 0) << std::endl;
Myfun *fptr = fn0.target<Myfun>();
std::cout << "val == " << (*fptr)(3) << std::endl;
std::function<int (int)> fn1;
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
std::cout << "no target == " << (fn1.target<Myfun>() == 0) << std::endl;
return (0);
}
empty == false
no target == false
val == -3
empty == true
no target == true
target_type
取得可呼叫物件的類型資訊。
const std::type_info& target_type() const;
備註
如果 *this 為空,成員函式會傳回 typeid(void),否則會傳回 typeid(T),其中 T 是目標物件的類型。
範例
// std__functional__function_target_type.cpp
// compile with: /EHsc
#include <functional>
#include <iostream>
int neg(int val)
{
return (-val);
}
int main()
{
std::function<int (int)> fn0(neg);
std::cout << std::boolalpha << "empty == " << !fn0 << std::endl;
std::cout << "type == " << fn0.target_type().name() << std::endl;
std::function<int (int)> fn1;
std::cout << std::boolalpha << "empty == " << !fn1 << std::endl;
std::cout << "type == " << fn1.target_type().name() << std::endl;
return (0);
}
empty == false
type == int (__cdecl*)(int)
empty == true
type == void