Bibliothèque fonctionnelle C ++ - opérateur ()

La description

Il appelle la cible de la fonction appelable stockée avec les paramètres args.

Déclaration

Voici la déclaration de std :: function :: function :: operator ()

R operator()( Args... args ) const;

C ++ 11

R operator()( Args... args ) const;

Paramètres

args - paramètres à transmettre à la cible de la fonction appelable stockée.

Valeur de retour

Il n'en renvoie aucun si R est nul. Sinon, la valeur de retour de l'appel de l'objet appelable stocké.

Des exceptions

noexcept: il ne lève aucune exception.

Exemple

Dans l'exemple ci-dessous pour std :: function :: operator ().

#include <iostream>
#include <functional>
 
void call(std::function<int()> f) {
   std::cout << f() << '\n';
}

int normal_function() {
   return 50;
}

int main() {
   int n = 4;
   std::function<int()> f = [&n](){ return n; };
   call(f);

   n = 5;
   call(f);

   f = normal_function;
   call(f);
}

La sortie devrait être comme ça -

4
5
50