Declaring functions

Da cppreference.com.
< cpp‎ | language

 
 
Linguaggio C + +
Temi generali
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Controllo del flusso
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Dichiarazioni esecuzione condizionale
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterazione dichiarazioni
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Vai dichiarazioni
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dichiarazione di funzione
lambda funzione dichiarazione
funzione di modello
specificatore inline
eccezioni specifiche(deprecato)
noexcept specificatore(C++11)
Eccezioni
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spazi dei nomi
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
sindacali tipi
tipi di funzione
decltype specifier(C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv specificatori
Durata di stoccaggio specificatori
constexpr specificatore(C++11)
specificatore auto(C++11)
alignas specificatore(C++11)
Inizializzazione
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Letterali
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Espressioni
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rappresentazioni alternative
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Tipo alias dichiarazione(C++11)
attributi(C++11)
Lancia
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversioni implicite
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Fusione C-stile e funzionale
Occupazione della memoria
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classi
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Specifiche per una classe di funzioni proprietà
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funzioni membro speciali
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Modelli
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
classe template
funzione di modello
modello di specializzazione
parametri confezioni(C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaggio in linea
 
Una dichiarazione di funzione introduce il nome della funzione e il suo tipo. Essa può apparire in qualsiasi ambito, ed è comunemente messo in file di intestazione.
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.
retname(params); (1)
retname(params)cvrefexceptattr; (2)
autoname(params)cvrefexceptattr->ret; (3) (dal C++11)
Una definizione di funzione fornisce il corpo di una funzione. Può apparire solo nel namespace o 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.
declname(params){body} (4)
attrdeclname(params)cvrefexceptattrvirttryinit-list{body} catch (5)
attrdeclname(params)cvrefexceptattr->retvirttryinit-list{body} catch (6) (dal C++11)
attrdeclname(params)cvrefexceptattrvirt= 0 ; (7)
attrdeclname(params)cvrefexceptattrvirt= default ; (8) (dal C++11)
attrdeclname(params)cvrefexceptattrvirt= delete ; (9) (dal C++11)

[modifica]Spiegazione

1)
Dichiarazione di funzione tipica
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)
Dichiarazione di funzione completa
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)
Dichiarazione di funzione completa con tipo di ritorno finale
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)
Tipico non membro funzione definizione
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)
Definizione di funzione completa
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)
Definizione di funzione completa con tipo di ritorno finale
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)
Pure funzione membro virtuale
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)
Funzione membro inadempiente
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)
Soppresso funzione membro
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) -
Sequenza opzionale di qualsiasi numero di funzione di attributi, come [[noreturn]] o [[carries_dependency]]. Può comparire sia prima che dopo il nome della funzione
Original:
Optional sequence of any number of funzione di attributi, 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 -
il tipo restituito dalla funzione, può essere void se la funzione restituisce nulla. Non può essere array o tipo di funzione, anche se può essere un puntatore o un riferimento a tale. Obbligatorio per tutte le funzioni ad eccezione di costruttori, distruttori, e gli operatori di conversione, che non devono fornire un tipo di ritorno .
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 -
dichiarazione di successione specificatore, che consiste di nessuno o alcune delle seguenti parole chiave: statica, extern, inline, virtual, explicit, friend, constexpr, in combinazione con il tipo restituito, ret
Original:
declaration specifier sequence, which consists of none or some of the following keywords: statica, extern, inline, 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| Opzionale const, volatile o constvolatile, applicabile solo ai non statici funzioni membro. Per una funzione membro di T classe, il tipo di [[cpp / lingua / questo| Optional const, volatile, or constvolatile, 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 constvolatile T* respectively. A member function declared const cannot modify members of *this.}}

ref(C++11) -
Opzionale & o &&, applicabile solo ai non statici altre funzioni membro di costruttori o distruttori. Per una funzione membro di T classe, il tipo del parametro oggetto implicito ai fini della risoluzione di sovraccarico sarà T& o T&& rispettivamente: a && qualificato funzione membro può essere chiamato solo su un'espressione oggetto 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 -
sia dinamico-specifica eccezioni o noexcept-specifica
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) -
Opzionale override o final, applicabile solo ai non statici funzioni membro
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) -
Trailing tipo di ritorno, applicabile solo se è retauto. Utile se il tipo dipende nomi degli argomenti, come template<class T, class U>auto add(T t, U u)-> decltype(t + u); o è complicato, come in 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 -
Costruttore inizializzatore lista, utilizzato solo nei costruttori
Original:
Costruttore inizializzatore lista, 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 -
Funzione opzionale blocco try. Se presente, catch deve essere fornito
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 -
Sequenza opzionale di catch-blocchi, applicabile solo su try viene utilizzato .
Original:
Optional sequence of catch-blocchi, 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 -
Il corpo della funzione, una (eventualmente vuoto) un'istruzione composta
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 lista dei parametri
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.


[modifica]Esempio 1: non membri funzioni

#include <iostream>#include <string>   // declaration in namespace(file) scope// (the definition is provided later)int f1();   // simple function with a default argument, returning nothingvoid f0(conststd::string& arg ="world"){std::cout<<"Hello, "<< arg <<'\n';}   // function returning a pointer to f0auto fp11()->void(*)(conststd::string&){return f0;}   // function returning a pointer to f0, pre-C++11 stylevoid(*fp03())(conststd::string&){return f0;}   int main(){ f0(); fp11()("test"); fp03()("again");int f2(std::string);// declaration in function scopestd::cout<< f2("bad12")<<'\n';}   // simple non-member function returning intint f1(){return42;}   // function with an exception specification and a function try blockint f2(std::string str)noexcepttry{returnstd::stoi(str);}catch(conststd::exception& e){std::cerr<<"stoi() failed!\n";return0;}

Output:

Hello, world Hello, test Hello, again stoi() failed! 0


[modifica]Esempio 2: funzioni membro

#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)virtualint 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(conststd::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 functionint 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(conststd::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=}

Output:

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