Bind-Funktion und Platzhalter in C++

Manchmal müssen wir den Betrieb einer Funktion je nach Bedarf manipulieren, z. B. einige Argumente auf die Standardwerte ändern usw. Eine zu habende Funktion vordefinieren Standardargumente schränkt die Vielseitigkeit einer Funktion ein und zwingt uns, die Standardargumente zu verwenden, und zwar jedes Mal mit ähnlichen Werten. Ab C++11 hat die Einführung der Bind-Funktion diese Aufgabe erleichtert. 

Wie funktioniert bind()?  

Die Bindungsfunktion mithilfe von Platzhaltern hilft dabei, die Position und Anzahl der von der Funktion zu verwendenden Werte zu manipulieren und die Funktion entsprechend der gewünschten Ausgabe zu ändern. 

Was sind Platzhalter?  

Platzhalter sind Namensräume, die die Position eines Werts in einer Funktion bestimmen. Sie werden vertreten durch _1 _2 _3 ... 

Beispiel:

CPP
   // C++ code to demonstrate bind() and   // placeholders   #include          #include         // for bind()   using     namespace     std  ;   // for placeholders   using     namespace     std  ::  placeholders  ;   // Driver function to demonstrate bind()   void     func  (  int     a       int     b       int     c  )   {      cout      < <     (  a     -     b     -     c  )      < <     endl  ;   }   int     main  ()   {      // for placeholders      using     namespace     std  ::  placeholders  ;      // Use of bind() to bind the function      // _1 is for first parameter and assigned      // to 'a' in above declaration.      // 2 is assigned to b      // 3 is assigned to c      auto     fn1     =     bind  (  func       _1       2       3  );      // 2 is assigned to a.      // _1 is for first parameter and assigned      // to 'b' in above declaration.      // 3 is assigned to c.      auto     fn2     =     bind  (  func       2       _1       3  );      // calling of modified functions      fn1  (  10  );      fn2  (  10  );      return     0  ;   }   

Ausgabe:

5 -11 

Im obigen Code hat bind() den Aufruf einer Funktion so geändert, dass er 1 Argument annimmt und die gewünschte Ausgabe zurückgibt. 

Eigenschaften von Platzhaltern

1. Die Position des Platzhalters bestimmt die Wertposition in der Funktionsaufrufanweisung 

CPP
   // C++ code to demonstrate placeholder   // property 1   #include          #include         // for bind()   using     namespace     std  ;   // for placeholders   using     namespace     std  ::  placeholders  ;   // Driver function to demonstrate bind()   void     func  (  int     a       int     b       int     c  )   {      cout      < <     (  a     -     b     -     c  )      < <     endl  ;   }   int     main     ()   {      // for placeholders      using     namespace     std  ::  placeholders  ;      // Second parameter to fn1() is assigned      // to 'a' in fun().      // 2 is assigned to 'b' in fun      // First parameter to fn1() is assigned      // to 'c' in fun().      auto     fn1     =     bind  (  func       _2       2       _1  );      // calling of function      cout      < <     'The value of function is : '  ;      fn1  (  1       13  );      // First parameter to fn2() is assigned      // to 'a' in fun().      // 2 is assigned to 'b' in fun      // Second parameter to fn2() is assigned      // to 'c' in fun().      auto     fn2     =     bind  (  func       _1       2       _2  );      // calling of same function      cout      < <     'The value of function after changing'      ' placeholder position is : '  ;      fn2  (  1       13  );      return     0  ;   }   

Ausgabe:

The value of function is : 10 The value of function after changing placeholder position is : -14 

Obwohl im obigen Code die Positionen von 1 und 13 in einem Funktionsaufruf gleich waren, änderte die Änderung der Position der Platzhalter die Art und Weise, wie die Funktion aufgerufen wurde.   

2. Die Anzahl der Platzhalter bestimmt die Anzahl der Argumente, die zur Übergabe der Funktion erforderlich sind.

Wir können jede beliebige Nr. verwenden. von Platzhaltern in der Funktionsaufrufanweisung (offensichtlich weniger als die maximale Anzahl von Argumenten). Die restlichen Werte werden durch die benutzerdefinierten Standardwerte ersetzt. 

CPP
   // C++ code to demonstrate placeholder   // property 2   #include         // for bind()   #include          using     namespace     std  ;   // for placeholders   using     namespace     std  ::  placeholders  ;   // Driver function to demonstrate bind()   void     func  (  int     a       int     b       int     c  )   {      cout      < <     (  a     -     b     -     c  )      < <     endl  ;   }   int     main  ()   {      // for placeholders      using     namespace     std  ::  placeholders  ;      // 1 placeholder      auto     fn1     =     bind  (  func       _1       2       4  );      // calling of function with 1 argument      cout      < <     'The value of function with 1 '      'placeholder is : '  ;      fn1  (  10  );      // 2 placeholders      auto     fn2     =     bind  (  func       _1       2       _2  );      // calling of function with 2 arguments      cout      < <     'The value of function with 2'      ' placeholders is : '  ;      fn2  (  13       1  );      // 3 placeholders      auto     fn3     =     bind  (  func       _1       _3       _2  );      // calling of function with 3 arguments      cout      < <     'The value of function with 3 '      'placeholders is : '  ;      fn3  (  13       1       4  );      return     0  ;   }   

Ausgabe:

The value of function with 1 placeholder is : 4 The value of function with 2 placeholders is : 10 The value of function with 3 placeholders is : 8 

Im obigen Code ist eindeutig die Nr. Anzahl der Platzhalter, die der Anzahl der Argumente entspricht, die zum Aufrufen der Funktion erforderlich sind. Die Bindung der Funktion richtet sich nach der Anzahl und Position der Platzhalter.