[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/language/function [Back]  [Original]

Declaring functions cppreference.com
cppreference.com
Espaces de noms
Variantes

Declaring functions

De cppreference.com

<metanoindex/>

 
 
Langage C++
Sujets gnraux
Contrle de flux
Instructions conditionnelles
Instructions d'itration
Instructions de saut
Fonctions
dclaration de fonction
expression lambda
fonction gnrique
spcificateur inline
spcification d'exception (obsolte)
spcificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spcificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littraux
Expressions
oprateurs alternatifs
Utilitaires
Types
dclaration typedef
dclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mmoire
Classes
Qualificatifs spcifiques aux membres de classe
Fonctions membres spciales
Modles
classes gnriques
fonctions gnriques
spcialisation de modles
paquets de paramtres (C++11)
Divers
Assembleur
 
Une dclaration de fonction introduit le nom de la fonction et son type. Il peut apparatre dans n'importe quel champ, et est gnralement plac dans les fichiers d'en-tte .
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 dfinition de fonction fournit l'organisme d'une fonction. Il peut seulement apparatre dans l'espace de noms ou de porte 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)
Dclaration 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)
Dclaration de fonction complte
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)
Dclaration 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 dfinition 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)
Dfinition de fonction complte
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)
Dfinition 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 dfaillant
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) -
Squence optionnelle de n'importe quel nombre de attributs de fonction, comme [[noreturn]] ou [[carries_dependency]]. Peut apparatre la fois avant et aprs le nom de la fonction
Original:
Optional sequence of any number of attributs de fonction, such as [[noreturn]] or [[carries_dependency]]. May appear both before and after the function name
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ret -
le type retourn par la fonction, peut-tre void si la fonction ne renvoie rien. Ne peut pas tre de type tableau ou fonction, bien que peut tre un pointeur ou une rfrence un tel. Requis pour toutes les fonctions, sauf les constructeurs, les destructeurs et les oprateurs de conversion, qui ne doit pas fournir un type de retour .
Original:
the type returned by the function, may be void if the function returns nothing. Cannot be array or function type, although can be a pointer or reference to such. Required for all functions except constructors, destructors, and conversion operators, which must not provide a return type.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decl -
dclaration spcificateur de squence, qui se compose d'aucun ou quelques-uns des mots-cls suivants: statique, extern, en ligne, virtual, explicit, friend, constexpr, combin avec le type de retour, ret
Original:
declaration specifier sequence, which consists of none or some of the following keywords: statique, extern, en ligne, virtual, explicit, friend, constexpr, combined with the return type, ret
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

{{par | cv |{{tr| En option const, volatile ou const volatile, ne s'applique qu'aux fonctions membres non statiques. Pour une fonction membre de la classe T, le type de la [[cpp / langue / ce| Optional const, volatile, or const volatile, only applicable to non-static member functions. For a member function of class T, the type of the [[cpp/language/this}}|this pointer]] will be const T*, volatile T*, or const volatile T* respectively. A member function declared const cannot modify members of *this.}}

ref (C++11) -
En option & ou &&, ne s'applique qu'aux fonctions membres non statiques autres que les constructeurs ou destructeurs. Pour une fonction membre de la classe T, le type du paramtre d'objet implicite dans le but de la rsolution de surcharge sera T& ou T&& respectivement: une fonction membre && qualifi ne peut tre appele sur un objet d'expression rvalue
Original:
Optional & or &&, only applicable to non-static member functions other than constructors or destructors. For a member function of class T, the type of the implicit object parameter for the purpose of overload resolution will be T& or T&& respectively: a &&-qualified member function can only be called on an rvalue object expression
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
except -
soit dynamiques exception de spcification ou noexcept-spcification
Original:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
virt (C++11) -
En option override ou final, ne s'applique qu'aux fonctions membres non statiques
Original:
Optional override or final, only applicable to non-static member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
->ret (C++11) -
Type de retour de fuite, ne s'applique que si ret est auto. Utile si le type dpend de noms d'arguments, comme template <class T, class U> auto add(T t, U u) -> decltype(t + u); ou est compliqu, comme dans auto fpif(int)->int(*)(int)
Original:
Trailing return type, only applicable if ret is auto. Useful if the type depends on argument names, such as template <class T, class U> auto add(T t, U u) -> decltype(t + u); or is complicated, such as in auto fpif(int)->int(*)(int)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
init-list -
Liste d'initialisation du constructeur, utilis uniquement par les constructeurs
Original:
Liste d'initialisation du constructeur, only used in constructors
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
try -
Option bloc try fonction. S'il est prsent, catch doit tre fournie
Original:
Optional function try block. If present, catch must be provided
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
catch -
Squence optionnelle de catch-blocs, applicable uniquement des try est utilis .
Original:
Optional sequence of catch-blocs, only applicable of try is used.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
body -
Le corps de la fonction, une instruction compose (ventuellement vide)
Original:
The body of the function, a (possibly empty) compound statement
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
params -
La liste des paramtres
Original:
The list of parameters
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


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;
}

Rsultat :

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=
}

Rsultat :

ctor1 called, data = 1
ctor2 called, data = 2
ctor2 failed, string was 'not a number'
2
ctor1 called, data = 3

Web Proxy Viewer  |  New URL  |  Original Page