Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

function::function

公共成员函数  <functional>

default/empty (1)
function() noexcept;
function (nullptr_t fn) noexcept;
initialization (2)
template <class Fn> function (Fn fn);
copy (3)
function (const function& x);
move (4)
function (function&& x);
with allocator (5)
template <class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc) noexcept;
template <class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc, nullptr_t fn) noexcept;
template <class Fn, class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc, Fn fn);
template <class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc, const function& x);
template <class Alloc>
  function (allocator_arg_t aa, const Alloc& alloc, function&& x);

构造函数包装器
构造一个function对象:

(1)default / empty
构造一个空函数对象(没有target)。

(2) initialization
该对象存储fn的一个decayed副本作为其target。
  • C++11: Fn应为可调用参数,并返回作为类模板参数指定的类型。
  • C++14: 如果参数fn不可调用,并且返回作为类模板参数指定的类型,则此构造函数不参与重载解析。

(3) copy constructor
该对象存储了x的 target (x.target())的副本。

(4) move constructor
该对象获得x的target。
X处于未指定但有效的状态。

(5) versions with allocator
与上面的版本相同,但对象存储alloc并在必要时使用它分配内部存储。 库的实现可以针对小型可调用对象进行优化(例如当目标是函数指针时),并且不为这些对象使用动态分配的内存。


☲  参数


fn
函数、函数指针、成员指针或任何类型的可复制构造函数对象(即类定义operator()的对象,包括closures和函数的其他实例化)。
如果fn是空指针、空成员指针或空函数对象,则该对象初始化为空函数。
否则,对象将初始化为fn的一个decayed副本(使用std::move(fn)进行内部初始化)。

x
相同类型的function对象(具有相同的签名,由其模板形参描述),其target被复制或移动到*this中。
如果x是一个空function对象,则该对象初始化为空函数。

aa
std:: allocator_arg值。此常量值仅用于选择带有allocator的构造参数。

alloc
如果需要,用于分配内部内存的Allocator对象。

☣  示例



// function example
#include <iostream>     // std::cout
#include <functional>   // std::function, std::negate

// a function:
int half(int x) {return x/2;}

// a function object class:
struct third_t {
  int operator()(int x) {return x/3;}
};

// a class with data members:
struct MyValue {
  int value;
  int fifth() {return value/5;}
};

int main () {
  std::function<int(int)> fn1 = half;                    // function
  std::function<int(int)> fn2 = ½                   // function pointer
  std::function<int(int)> fn3 = third_t();               // function object
  std::function<int(int)> fn4 = [](int x){return x/4;};  // lambda expression
  std::function<int(int)> fn5 = std::negate<int>();      // standard function object

  std::cout << "fn1(60): " << fn1(60) << '\n';
  std::cout << "fn2(60): " << fn2(60) << '\n';
  std::cout << "fn3(60): " << fn3(60) << '\n';
  std::cout << "fn4(60): " << fn4(60) << '\n';
  std::cout << "fn5(60): " << fn5(60) << '\n';

  // stuff with members:
  std::function<int(MyValue&)> value = &MyValue::value;  // pointer to data member
  std::function<int(MyValue&)> fifth = &MyValue::fifth;  // pointer to member function

  MyValue sixty {60};

  std::cout << "value(sixty): " << value(sixty) << '\n';
  std::cout << "fifth(sixty): " << fifth(sixty) << '\n';

  return 0;
}

输出:
fn1(60): 30
fn2(60): 30
fn3(60): 20
fn4(60): 15
fn5(60): -60
value(sixty): 60
fifth(sixty): 12

☂ 异常安全性



如果目标是函数指针或指向可调用对象的reference_wrapper,它永远不会抛出异常(no-throw保证)。
否则,它只能在目标可调用对象的复制构造或移动构造引发时抛出,或者在分配内存时抛出异常(如bad_alloc)时抛出。

🍄  另请参阅



function::operator= 赋值函数对象(公共成员函数)
swap(function) 交换目标(公共成员函数)

联系我们 免责声明 关于CandCplus 网站地图