binary_function Structure Sample

Muestra cómo utilizar la estructura de binary_function en la biblioteca de (STL) plantillas estándar en Visual C++.

template<class _A1, class _A2, class _R>
   struct binary_function
   {
      typedef _A1 first_argument_type;
      typedef _A2 second_argument_type;
      typedef _R result_type;
   };

Comentarios

[!NOTA]

La clase y los nombres de parámetro en el prototipo no coincide con la versión del archivo de encabezado.Algunos se han modificado para mejorar la legibilidad.

La clase ENT0ENT de binary_functionse utiliza como una clase base para permitir que el usuario fácilmente defina las funciones de operador binario que usan los tipos de datos A y b como argumentos y objetos de C del tipo de datos devuelto.

Ejemplo

// binfunc.cpp
// compile with: /EHsc
//
// Structure used: binary_function<A,B,C> - base
//                 class used to create operator
//                 functions taking data types A
//                 and B and returning data type C.

#include <functional>
#include <iostream>

using namespace std ;

class binary_test : public binary_function<binary_test &,int,float>
{
public:
  float value;
  binary_test(){value=10.0;}
  binary_test(float x){value=x;}
  result_type operator<<(second_argument_type arg2);
};

binary_test::result_type
binary_test::operator<<(binary_test::second_argument_type arg2)
{
  value = (float)(((int)value) << arg2);
  cout << "New value after shift is " << value << endl;
  return value;
}

int main(void)
{
  binary_test item;

  cout << "Begin" << endl;
  item = item << 2;
}

Output

Begin
New value after shift is 40

Requisitos

encabezado: < > funcional

Vea también

Conceptos

Ejemplos de biblioteca de plantillas estándar