STL-functional-2

Classes

Wrapper classes

Wrapper classes are classes that hold an object and have an interface similar to that object, but adding or changing some of its features:

binary_negate

template <class Predicate> class binary_negate;

Negate binary function object class

二元函数对象类,其调用返回与传递给其构造函数的另一个二元函数相反的值。 类型为binary_enegate的对象通常使用函数not2构造。 类的定义行为与以下行为相同:

template <class Predicate> class binary_negate
{
protected:
  Predicate fn_;
public:
  explicit binary_negate (const Predicate& pred) : fn_ (pred) {}
  bool operator() (const typename Predicate::argument_type& x) const {return !fn_(x,y);}
  typedef typename Predicate::first_argument_type  first_argument_type;
  typedef typename Predicate::second_argument_type second_argument_type;
  typedef bool result_type;
};
// binary_negate example
#include <iostream>     // std::cout
#include <functional>   // std::binary_negate, std::equal_to
#include <algorithm>    // std::mismatch
#include <utility>      // std::pair

int main () {
  std::equal_to<int> equality;
  std::binary_negate < std::equal_to<int> > nonequality (equality);
  int foo[] = {10,20,30,40,50};
  int bar[] = {0,15,30,45,60};
  std::pair<int*,int*> firstmatch,firstmismatch;
  firstmismatch = std::mismatch (foo,foo+5,bar,equality);
  firstmatch = std::mismatch (foo,foo+5,bar,nonequality);
  std::cout << "First mismatch in bar is " << *firstmismatch.second << "\n";
  std::cout << "First match in bar is " << *firstmatch.second << "\n";
  return 0;
}Output:
First mismatch in bar is 0
First match in bar is 30

function

template <class T> function;     // undefinedtemplate <class Ret, class... Args> class function<Ret(Args...)>;

Function wrapper

类,该类可以将任何类型的可调用元素(如函数和函数对象)包装成可复制对象,并且其类型仅取决于其调用签名(而不取决于可调用元素类型本身)。

函数类实例化的对象可以包装以下任何类型的可调用对象:函数、函数指针、指向成员的指针或任何类型的函数对象(即,其类定义运算符()的对象,包括闭包)。

包装的可调用对象的衰减副本由对象内部存储,该对象成为函数的目标。不需要此目标可调用对象的特定类型来实例化函数包装类;只有其呼叫签名。

函数对象可以被复制和移动,并且可以用于直接调用具有指定调用签名的可调用对象(请参见成员运算符())。

函数对象也可以处于没有目标可调用对象的状态。在这种情况下,它们被称为空函数,调用它们会引发bad_function_call异常。

reference_wrapper

template <class T> class reference_wrapper;

Reference wrapper

Class that emulates a reference to an element of type T, but which can be copied (it is both copy-constructible and copy-assignable).

// reference_wrapper example:
#include <iostream>     // std::cout
#include <functional>   // std::reference_wrapper

int main () {
  int a(10),b(20),c(30);

  // an array of "references":
  std::reference_wrapper<int> refs[] = {a,b,c};

  std::cout << "refs:";
  for (int& x : refs) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}Possible output:
refs: 10 20 30

unary_negate

template <class Predicate> class unary_negate;

Negate unary function object class

一元函数对象类,其调用返回与传递给其构造函数的另一个一元函数相反的结果。

类型为unary_onegate的对象通常使用函数not1构造。

类的定义行为与以下行为相同:

template <class Predicate> class unary_negate
{
protected:
  Predicate fn_;
public:
  explicit unary_negate (const Predicate& pred) : fn_ (pred) {}
  bool operator() (const typename Predicate::argument_type& x) const {return !fn_(x);}
  typedef typename Predicate::argument_type argument_type;
  typedef bool result_type;
};
// unary_negate example
#include <iostream>     // std::cout
#include <functional>   // std::unary_negate
#include <algorithm>    // std::count_if

struct IsOdd_class {
  bool operator() (const int& x) const {return x%2==1;}
  typedef int argument_type;
} IsOdd_object;

int main () {
  std::unary_negate<IsOdd_class> IsEven_object (IsOdd_object);
  int values[] = {1,2,3,4,5};
  int cx;
  cx = std::count_if ( values, values+5, IsEven_object );
  std::cout << "There are " << cx << " elements with even values.\n";
  return 0;
}Output:
There are 2 elements with even values.

Operator classes

Operator classes are classes that define functional objects that call operators:

bit_and

template <class T> struct bit_and;

Bitwise AND function object class

二元函数对象,其调用返回在其两个参数之间应用按位“and”运算的结果(由运算符&返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct bit_and {
  T operator() (const T& x, const T& y) const {return x&y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};
// bit_and example
#include <iostream>     // std::cout
#include <functional>   // std::bit_and
#include <algorithm>    // std::transform
#include <iterator>     // std::end


int main () {
  int values[] = {100,200,300,400,500};
  int masks[] = {0xf,0xf,0xf,255,255};
  int results[5];

  std::transform (values, std::end(values), masks, results, std::bit_and<int>());

  std::cout << "results:";
  for (const int& x: results)
    std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}Output:
results: 4 8 12 144 244

bit_or

template <class T> struct bit_or;

Bitwise OR function object class

二元函数对象类,其调用返回在其两个参数之间应用按位“或”运算的结果(由运算符|返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct bit_or {
  T operator() (const T& x, const T& y) const {return x|y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};
// bit_or example
#include <iostream>     // std::cout
#include <functional>   // std::bit_or
#include <numeric>      // std::accumulate
#include <iterator>     // std::end

int main () {
  int flags[] = {1,2,4,8,16,32,64,128};
  int acc = std::accumulate (flags, std::end(flags), 0, std::bit_or<int>());
  std::cout << "accumulated: " << acc << '\n';
  return 0;
}Output:
accumulated: 255

bit_xor

template <class T> struct bit_xor;

Bitwise XOR function object class

二元函数对象类,其调用返回在其两个参数之间应用逐位“异或”运算的结果(由运算符^返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct bit_xor {
  T operator() (const T& x, const T& y) const {return x^y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};
// bit_xor example
#include <iostream>     // std::cout
#include <functional>   // std::bit_xor
#include <algorithm>    // std::accumulate
#include <iterator>     // std::end

int main () {
  int flags[] = {1,2,3,4,5,6,7,8,9,10};
  int acc = std::accumulate (flags, std::end(flags), 0, std::bit_xor<int>());
  std::cout << "xor: " << acc << '\n';
  return 0;
}Output:
xor: 11

divides

template <class T> struct divides;

Division function object class

二元函数对象类,其调用返回第一个参数除以第二个参数的结果(由运算符/返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct divides {
  T operator() (const T& x, const T& y) const {return x/y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};
// divides example
#include <iostream>     // std::cout
#include <functional>   // std::divides
#include <algorithm>    // std::transform

int main () {
  int first[]={10,40,90,40,10};
  int second[]={1,2,3,4,5};
  int results[5];
  std::transform (first, first+5, second, results, std::divides<int>());
  for (int i=0; i<5; i++)
    std::cout << results[i] << ' ';
  std::cout << '\n';
  return 0;
}Output:
10 20 30 10 2

equal_to

template <class T> struct equal_to;

Function object class for equality comparison

二元函数对象类,其调用返回其两个参数是否相等(由运算符==返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct equal_to {
  bool operator() (const T& x, const T& y) const {return x==y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};
// equal_to example
#include <iostream>     // std::cout
#include <utility>      // std::pair
#include <functional>   // std::equal_to
#include <algorithm>    // std::mismatch

int main () {
  std::pair<int*,int*> ptiter;
  int foo[]={10,20,30,40,50};
  int bar[]={10,20,40,80,160};
  ptiter = std::mismatch (foo, foo+5, bar, std::equal_to<int>());
  std::cout << "First mismatching pair is: " << *ptiter.first;
  std::cout << " and " << *ptiter.second << '\n';
  return 0;
}Output:
First mismatching pair is: 30 and 40

greater

template <class T> struct greater;

Function object class for greater-than inequality comparison

二元函数对象类,其调用返回其第一个参数是否大于第二个参数(由运算符>返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct greater {
  bool operator() (const T& x, const T& y) const {return x>y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};
// greater example
#include <iostream>     // std::cout
#include <functional>   // std::greater
#include <algorithm>    // std::sort

int main () {
  int numbers[]={20,40,50,10,30};
  std::sort (numbers, numbers+5, std::greater<int>());
  for (int i=0; i<5; i++)
    std::cout << numbers[i] << ' ';
  std::cout << '\n';
  return 0;
}
Output:
50 40 30 20 10

greater_equal

template <class T> struct greater_equal;

Function object class for greater-than-or-equal-to comparison

二元函数对象类,其调用返回其第一个参数是否大于或等于第二个参数(由运算符>=返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct greater_equal {
  bool operator() (const T& x, const T& y) const {return x>=y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};
// greater_equal example
#include <iostream>     // std::cout
#include <functional>   // std::greater_equal, std::bind2nd
#include <algorithm>    // std::count_if

int main () {
  int numbers[]={20,-30,10,-40,0};
  int cx = std::count_if (numbers, numbers+5, std::bind2nd(std::greater_equal<int>(),0));
  std::cout << "There are " << cx << " non-negative elements.\n";
  return 0;
}Output:
There are 3 non-negative elements.

less

template <class T> struct less;

Function object class for less-than inequality comparison

二元函数对象类,其调用返回其第一个参数是否小于第二个参数(由运算符<返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct less {
  bool operator() (const T& x, const T& y) const {return x<y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};
// less example
#include <iostream>     // std::cout
#include <functional>   // std::less
#include <algorithm>    // std::sort, std::includes

int main () {
  int foo[]={10,20,5,15,25};
  int bar[]={15,10,20};
  std::sort (foo, foo+5, std::less<int>());  // 5 10 15 20 25
  std::sort (bar, bar+3, std::less<int>());  //   10 15 20
  if (std::includes (foo, foo+5, bar, bar+3, std::less<int>()))
    std::cout << "foo includes bar.\n";
  return 0;
}Output:
foo includes bar.

less_equal

template <class T> struct less_equal;

Function object class for less-than-or-equal-to comparison

二元函数对象类,其调用返回其第一个参数是否小于或等于第二个参数(由运算符<=返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。

此成员函数允许使用与函数调用相同的语法来使用对象。 它的定义与以下行为相同:

template <class T> struct less_equal {
  bool operator() (const T& x, const T& y) const {return x<=y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};
// less_equal example
#include <iostream>     // std::cout
#include <functional>   // std::less_equal, std::bind2nd
#include <algorithm>    // std::count_if

int main () {
  int numbers[]={25,50,75,100,125};
  int cx = std::count_if (numbers, numbers+5, std::bind2nd(std::less_equal<int>(),100));
  std::cout << "There are " << cx << " elements lower than or equal to 100.\n";
  return 0;
}Output:
There are 4 elements lower or equal to 100.

logical_and

template <class T> struct logical_and;

Logical AND function object class

二元函数对象类,其调用返回其两个参数之间的逻辑“and”运算的结果(由运算符&&返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct logical_and {
  bool operator() (const T& x, const T& y) const {return x&&y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};
// logical_and example
#include <iostream>     // std::cout, std::boolalpha
#include <functional>   // std::logical_and
#include <algorithm>    // std::transform

int main () {
  bool foo[] = {true,false,true,false};
  bool bar[] = {true,true,false,false};
  bool result[4];
  std::transform (foo, foo+4, bar, result, std::logical_and<bool>());
  std::cout << std::boolalpha << "Logical AND:\n";
  for (int i=0; i<4; i++)
    std::cout << foo[i] << " AND " << bar[i] << " = " << result[i] << "\n";
  return 0;
}Output:
Logical AND:
true AND true = true
false AND true = false
true AND false = false
false AND false = false

logical_not

template <class T> struct logical_not;

Logical NOT function object class

一元函数对象类,其调用返回其参数的逻辑“not”运算的结果(由运算符返回!)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct logical_not {
  bool operator() (const T& x) const {return !x;}
  typedef T argument_type;
  typedef bool result_type;
};
// logical_not example
#include <iostream>     // std::cout, std::boolalpha
#include <functional>   // std::logical_not
#include <algorithm>    // std::transform

int main () {
  bool values[] = {true,false};
  bool result[2];
  std::transform (values, values+2, result, std::logical_not<bool>());
  std::cout << std::boolalpha << "Logical NOT:\n";
  for (int i=0; i<2; i++)
    std::cout << "NOT " << values[i] << " = " << result[i] << "\n";
  return 0;
}Output:
Logical NOT:
NOT true = false
NOT false = true

logical_or

template <class T> struct logical_or;

Logical OR function object class

二元函数对象类,其调用返回其两个参数之间的逻辑“或”运算的结果(由运算符||返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct logical_or {
  bool operator() (const T& x, const T& y) const {return x||y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};
// logical_or example
#include <iostream>     // std::cout, std::boolalpha
#include <functional>   // std::logical_or
#include <algorithm>    // std::transform

int main () {
  bool foo[] = {true,false,true,false};
  bool bar[] = {true,true,false,false};
  bool result[4];
  std::transform (foo, foo+4, bar, result, std::logical_or<bool>());
  std::cout << std::boolalpha << "Logical OR:\n";
  for (int i=0; i<4; i++)
    std::cout << foo[i] << " OR " << bar[i] << " = " << result[i] << "\n";
  return 0;
}Output:
Logical OR:
true OR true = true
false OR true = true
true OR false = true
false OR false = false

minus

template <class T> struct minus;

Subtraction function object class

二元函数对象类,其调用返回从第一个参数减去第二个参数的结果(由二进制运算符-返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct minus {
  T operator() (const T& x, const T& y) const {return x-y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};
// minus example
#include <iostream>     // std::cout
#include <functional>   // std::minus
#include <algorithm>    // std::transform

int main () {
  int numbers[]={10,20,30};
  int result;
  result = std::accumulate (numbers, numbers+3, 100, std::minus<int>());
  std::cout << "The result of 100-10-20-30 is " << result << ".\n";
  return 0;
}Output:
The result of 100-10-20-30 is 40.

modulus

template <class T> struct modulus;

Modulus function object class

二元函数对象类,其调用返回其两个参数之间的模运算结果(由运算符%返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct modulus {
  T operator() (const T& x, const T& y) const {return x%y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};
// modulus example
#include <iostream>     // std::cout
#include <functional>   // std::modulus, std::bind2nd
#include <algorithm>    // std::transform

int main () {
  int numbers[]={1,2,3,4,5};
  int remainders[5];
  std::transform (numbers, numbers+5, remainders, std::bind2nd(std::modulus<int>(),2));
  for (int i=0; i<5; i++)
    std::cout << numbers[i] << " is " << (remainders[i]==0?"even":"odd") << '\n';
  return 0;
}
Output:
1 is odd
2 is even
3 is odd
4 is even
5 is odd

multiplies

template <class T> struct multiplies;

Multiplication function object class

二元函数对象类,其调用返回其两个参数相乘的结果(由运算符*返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct multiplies {
  T operator() (const T& x, const T& y) const {return x*y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};
// factorials (multiplies example)
#include <iostream>     // std::cout
#include <functional>   // std::multiplies
#include <numeric>      // std::partial_sum

int main () {
  int numbers[9];
  int factorials[9];
  for (int i=0;i<9;i++) numbers[i]=i+1;
  std::partial_sum (numbers, numbers+9, factorials, std::multiplies<int>());
  for (int i=0; i<9; i++)
    std::cout << numbers[i] << "! is " << factorials[i] << '\n';
  return 0;
}Output:
1! is 1
2! is 2
3! is 6
4! is 24
5! is 120
6! is 720
7! is 5040
8! is 40320
9! is 362880

negate

template <class T> struct negate;

Negative function object class

一元函数对象类,其调用返回否定其参数的结果(由一元运算符-返回)。

对一个值取反将返回相同的值,但符号相反。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct negate {
  T operator() (const T& x) const {return -x;}
  typedef T argument_type;
  typedef T result_type;
};
// negate example
#include <iostream>     // std::cout
#include <functional>   // std::negate
#include <algorithm>    // std::transform

int main () {
  int numbers[]={10,-20,30,-40,50};
  std::transform (numbers, numbers+5, numbers, std::negate<int>());
  for (int i=0; i<5; i++)
    std::cout << numbers[i] << ' ';
  std::cout << '\n';
  return 0;
}
Output:
-10 20 -30 40 -50

not_equal_to

template <class T> struct not_equal_to;

Function object class for non-equality comparison

二元函数对象类,其调用返回其两个参数是否比较不相等(由运算符运算符返回!=)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。

此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct not_equal_to {
  bool operator() (const T& x, const T& y) const {return x!=y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};
// not_equal_to example
#include <iostream>     // std::cout
#include <functional>   // std::not_equal_to
#include <algorithm>    // std::adjacent_find

int main () {
  int numbers[]={10,10,10,20,20};
  int* pt = std::adjacent_find (numbers, numbers+5, std::not_equal_to<int>()) +1;
  std::cout << "The first different element is " << *pt << '\n';
  return 0;
}Output:
The first different element is 20

plus

template <class T> struct plus;

Addition function object class

二元函数对象类,其调用返回添加其两个参数的结果(由运算符+返回)。

一般来说,函数对象是定义了成员函数运算符()的类的实例。此成员函数允许使用与函数调用相同的语法来使用对象。

它的定义与以下行为相同:

template <class T> struct plus {
  T operator() (const T& x, const T& y) const {return x+y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};
// plus example
#include <iostream>     // std::cout
#include <functional>   // std::plus
#include <algorithm>    // std::transform

int main () {
  int first[]={1,2,3,4,5};
  int second[]={10,20,30,40,50};
  int results[5];
  std::transform (first, first+5, second, results, std::plus<int>());
  for (int i=0; i<5; i++)
	std::cout << results[i] << ' ';
  std::cout << '\n';
  return 0;
}Output:
11 22 33 44 55

Other classes

bad_function_call

class bad_function_call;

Exception thrown on bad call

在调用空函数对象的函数调用时由其抛出的类型。

空函数对象是没有目标可调用对象的函数对象。

此类派生自异常。有关标准异常的成员定义,请参见异常类。

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

int main () {
  std::function<int(int,int)> foo = std::plus<int>();
  std::function<int(int,int)> bar;

  try {
    std::cout << foo(10,20) << '\n';
    std::cout << bar(10,20) << '\n';
  }
  catch (std::bad_function_call& e)
  {
    std::cout << "ERROR: Bad function call\n";
  }

  return 0;
}Output:
30
ERROR: Bad function call

hash

template <class T> struct hash;

Default hash function object class

一元函数对象类,用于定义标准库使用的默认哈希函数。

函数调用返回其参数的哈希值:哈希值是一个仅依赖于其参数的值,对于相同的参数(对于给定的程序执行)总是返回相同的值。返回的值与为不同参数返回的值相同的可能性很小(碰撞的可能性接近1/numeric_limits<size_t>:max)。

其他函数对象类型可以用作无序容器的哈希,前提是它们的行为如上所述,并且它们至少是可复制的、可破坏的函数对象。

默认的hash是一个模板类,它不是为一般情况定义的。但是,所有库实现都至少提供以下特定于类型的专业化:

除了可以用适当类型的参数调用外,哈希实例化的所有对象都是默认可构造的、可复制可构造的,可复制可分配的、可破坏的和可交换的。

用户可以使用这些相同的属性为此模板提供自定义专业化。

// hash example
#include <iostream>
#include <functional>
#include <string>

int main ()
{
  char nts1[] = "Test";
  char nts2[] = "Test";
  std::string str1 (nts1);
  std::string str2 (nts2);

  std::hash<char*> ptr_hash;
  std::hash<std::string> str_hash;

  std::cout << "same hashes:\n" << std::boolalpha;
  std::cout << "nts1 and nts2: " << (ptr_hash(nts1)==ptr_hash(nts2)) << '\n';
  std::cout << "str1 and str2: " << (str_hash(str1)==str_hash(str2)) << '\n';

  return 0;
}Output:
same hashes:
nts1 and nts2: false
str1 and str2: true

is_bind_expression

template <class T> struct is_bind_expression;

Is bind expression

用于标识T是否为绑定表达式的Trait类。

bind函数使用这个特性来确定其每个参数的类型是否是子表达式(即,它本身是否是bind返回的类型)。用户可以将此模板专门用于将被视为绑定子表达式的类型。

// is_bind_expression example
#include <iostream>     // std::cout, std::boolalpha
#include <functional>   // std::bind, std::plus, std::placeholders, std::is_bind_expression

int main () {
  using namespace std::placeholders;  // introduces _1
  auto increase_int = std::bind (std::plus<int>(),_1,1);

  std::cout << std::boolalpha;
  std::cout << std::is_bind_expression<decltype(increase_int)>::value << '\n';

  return 0;
}Output:
true

is_placeholder

template <class T> struct is_placeholder;

Is placeholder

用于标识T是否为绑定占位符的Trait类。

bind函数使用此特性来确定其每个参数的类型是否为占位符。用户可以将此模板专门用于将被视为占位符的类型。

// is_placeholder example
#include <iostream>     // std::cout, std::boolalpha
#include <functional>   // std::is_placeholder, std::placeholders

int main () {
  using namespace std::placeholders;  // introduces _1

  std::cout << std::is_placeholder<decltype(_1)>::value << '\n';
  std::cout << std::is_placeholder<decltype(_2)>::value << '\n';
  std::cout << std::is_placeholder<int>::value << '\n';

  return 0;
}Output:
1
2
0

placeholders

namespace placeholders {  extern /* unspecified */ _1;  extern /* unspecified */ _2;  extern /* unspecified */ _3;  // ...}

Bind argument placeholders

此命名空间声明了未指定数量的对象:_1,_2,_3,。。。,用于指定函数绑定调用中的占位符。

当调用bind返回的函数对象时,占位符为_1的参数会被调用中的第一个参数替换,_2会被调用的第二个参数替换等等…例如:

using namespace std::placeholders;
auto bound_fn = std::bind (fn,100,_1);
bound_fn(5);  // calls fn(100,5), i.e.: replacing _1 by the first argument: 5 

这些占位符对象的类型是未指定的(这取决于库实现,请参见is_placeholder),但在所有情况下,它们的类型至少应为nothrow默认可构造和nothrow副本可构造。是否支持赋值操作或附加构造函数是由实现定义的,但任何复制赋值或移动构造函数也不应抛出。

当一个绑定调用被用作另一个绑定的调用中的子表达式时,占位符是相对于最外层的绑定表达式的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值