Declaring functions — cppreference.com
De cppreference.com
<metanoindex/>
Une déclaration de fonction introduit le nom de la fonction et son type. Il peut apparaître dans n'importe quel champ, et est généralement placé dans les fichiers d'en-tête .
Original:
A function declaration introduces the function name and its type. It may appear in any scope, and is commonly placed in header files.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ret name ( params ) ;
|
(1) | ||||||||
ret name ( params ) cv ref except attr ;
|
(2) | ||||||||
auto name ( params ) cv ref except attr -> ret ;
|
(3) | (depuis C++11) | |||||||
Une définition de fonction fournit à l'organisme d'une fonction. Il peut seulement apparaître dans l'espace de noms ou de portée de la classe .
Original:
A function definition provides the body of a function. It may only appear in namespace or class scope.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decl name ( params ) { body }
|
(4) | ||||||||
attr decl name ( params ) cv ref except attr virt try init-list { body } catch
|
(5) | ||||||||
attr decl name ( params ) cv ref except attr -> ret virt try init-list { body } catch
|
(6) | (depuis C++11) | |||||||
attr decl name ( params ) cv ref except attr virt = 0 ;
|
(7) | ||||||||
attr decl name ( params ) cv ref except attr virt = default ;
|
(8) | (depuis C++11) | |||||||
attr decl name ( params ) cv ref except attr virt = delete ;
|
(9) | (depuis C++11) | |||||||
Explication
1)
Déclaration de la fonction typique
Original:
Typical function declaration
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Déclaration de fonction complète
Original:
Comprehensive function declaration
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
Déclaration de la fonction globale avec le type de retour de fuite
Original:
Comprehensive function declaration with trailing return type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
Typique définition d'une fonction non-membre
Original:
Typical non-member function definition
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
Définition de fonction complète
Original:
Comprehensive function definition
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
Définition de la fonction globale avec le type de retour de fuite
Original:
Comprehensive function definition with trailing return type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
7)
Fonction membre virtuelle pure
Original:
Pure virtual member function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
8)
Fonction membre défaillant
Original:
Defaulted member function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
9)
Supprimé fonction membre
Original:
Deleted member function
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
| attr (C++11) | - | Séquence optionnelle de n'importe quel nombre de attributs de fonction, comme Original: Optional sequence of any number of attributs de fonction, such as The text has been machine-translated via Google Translate. |
| ret | - | le type retourné par la fonction, peut-être Original: the type returned by the function, may be The text has been machine-translated via Google Translate. |
| decl | - | déclaration spécificateur de séquence, qui se compose d'aucun ou quelques-uns des mots-clés suivants: statique, extern, en ligne, Original: declaration specifier sequence, which consists of none or some of the following keywords: statique, extern, en ligne, The text has been machine-translated via Google Translate. {{par | cv |{{tr| En option |
| ref (C++11) | - | En option Original: Optional The text has been machine-translated via Google Translate. |
| except | - | soit dynamiques exception de spécification ou noexcept-spécification Original: The text has been machine-translated via Google Translate. |
| virt (C++11) | - | En option Original: Optional The text has been machine-translated via Google Translate. |
->ret (C++11)
|
- | Type de retour de fuite, ne s'applique que si ret est Original: Trailing return type, only applicable if ret is The text has been machine-translated via Google Translate. |
| init-list | - | Liste d'initialisation du constructeur, utilisé uniquement par les constructeurs Original: The text has been machine-translated via Google Translate. |
try
|
- | Option bloc try fonction. S'il est présent, catch doit être fournie Original: Optional function try block. If present, catch must be provided The text has been machine-translated via Google Translate. |
| catch | - | Séquence optionnelle de catch-blocs, applicable uniquement des Original: Optional sequence of catch-blocs, only applicable of The text has been machine-translated via Google Translate. |
| body | - | Le corps de la fonction, une instruction composée (éventuellement vide) Original: The body of the function, a (possibly empty) compound statement The text has been machine-translated via Google Translate. |
| params | - | La liste des paramètres Original: The text has been machine-translated via Google Translate. |
Exemple 1: fonctions non-membres
#include <iostream> #include <string> // declaration in namespace(file) scope // (the definition is provided later) int f1(); // simple function with a default argument, returning nothing void f0(const std::string& arg = "world") { std::cout << "Hello, " << arg << '\n'; } // function returning a pointer to f0 auto fp11() -> void(*)(const std::string&) { return f0; } // function returning a pointer to f0, pre-C++11 style void (*fp03())(const std::string&) { return f0; } int main() { f0(); fp11()("test"); fp03()("again"); int f2(std::string); // declaration in function scope std::cout << f2("bad12") << '\n'; } // simple non-member function returning int int f1() { return 42; } // function with an exception specification and a function try block int f2(std::string str) noexcept try { return std::stoi(str); } catch(const std::exception& e) { std::cerr << "stoi() failed!\n"; return 0; }
Résultat :
Hello, world Hello, test Hello, again stoi() failed! 0
Exemple 2: les fonctions membres
#include <iostream> #include <string> #include <utility> #include <exception> struct S { int data; // simple converting constructor (declaration) S(int val); // simple explicit constructor (declaration) explicit S(std::string str); // const member function (definition) virtual int getData() const { return data; } }; // definition of the constructor S::S(int val) : data(val) { std::cout << "ctor1 called, data = " << data << '\n'; } // this constructor has a catch clause S::S(std::string str) try : data(std::stoi(str)) { std::cout << "ctor2 called, data = " << data << '\n'; } catch(const std::exception&) { std::cout << "ctor2 failed, string was '" << str << "'\n"; throw; // ctor's catch clause should always rethrow } struct D : S { int data2; // constructor with a default argument D(int v1, int v2 = 11) : S(v1), data2(v2) {} // virtual member function int getData() const override { return data*data2; } // lvalue-only assignment operator D& operator=(D other) & { std::swap(other.data, data); std::swap(other.data2, data2); return *this; } }; int main() { D d1 = 1; S s2("2"); try { S s3("not a number"); } catch(const std::exception&) {} std::cout << s2.getData() << '\n'; D d2(3, 4); d2 = d1; // OK: assignment to lvalue // D(5) = d1; // ERROR: no suitable overload of operator= }
Résultat :
ctor1 called, data = 1 ctor2 called, data = 2 ctor2 failed, string was 'not a number' 2 ctor1 called, data = 3