auto specifier (dal C++11)
Da cppreference.com.
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Specifica che il tipo della variabile che viene dichiarata sarà automaticamente dedotto dalla sua inizializzatore. Per le funzioni, specifica che il tipo restituito è un tipo di ritorno finale.
Original:
Specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a 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.
You can help to correct and verify the translation. Click here for instructions.
Indice |
[modifica] Sintassi
auto variable initializer (dal C++11) | |||||||||
auto function -> return type (dal C++11) | |||||||||
[modifica] Spiegazione
1)Quando si dichiarano variabili in ambito blocco, in spazio dei nomi, nelle dichiarazioni di init per loop, ecc, il tipo della variabile può essere omesso e la parola chiave auto può essere utilizzato.
Original:
When declaring variables in block scope, in namespace scope, in init statements of for loops, etc, the type of the variable may be omitted and the keyword auto may be used instead.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Una volta che il tipo di inizializzatore è stata determinata, il compilatore determina il tipo che sostituirà la parola chiave auto come utilizzando le regole di deduzione argomento di modello da una chiamata di funzione. Il auto parola chiave può essere accompagnata da modifica, come ad esempio const o &, che parteciperanno alla detrazione tipo. Ad esempio, dato const auto& i = expr;, il tipo di
2) i
è esattamente il tipo di u
argomento in un modello template<class U> void f(const U& u) immaginaria f(expr) se la chiamata di funzione è stato compilato.Original:
Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto as if using the rules for template argument deduction from a function call. The keyword auto may be accompanied by modifies, such as const or &, which will participate in the type deduction. For example, given const auto& i = expr;, the type of
i
is exactly the type of the argument u
in an imaginary template template<class U> void f(const U& u) if the function call f(expr) was compiled.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
In un dichiarazione di funzione, il auto parola chiave non eseguire il rilevamento automatico del tipo. Serve solo come parte della sintassi ritorno finale tipo.
Original:
In a dichiarazione di funzione, the keyword auto does not perform automatic type detection. It only serves as a part of the trailing return type syntax.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Note
Fino al C + +11, auto avuto la semantica di un Durata di stoccaggio specificatore.
Original:
Until C++11, auto had the semantic of a Durata di stoccaggio specificatore.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[modifica] Esempio
#include <iostream> #include <cmath> #include <typeinfo> template<class T, class U> auto add(T t, U u) -> decltype(t + u) // the return type of add is the type of operator+(T,U) { return t + u; } auto get_fun(int arg)->double(*)(double) // same as double (*get_fun(int))(double) { switch (arg) { case 1: return std::fabs; case 2: return std::sin; default: return std::cos; } } int main() { auto a = 1 + 2; std::cout << "type of a: " << typeid(a).name() << '\n'; auto b = add(1, 1.2); std::cout << "type of b: " << typeid(b).name() << '\n'; //auto int c; //compile-time error auto d = {1, 2}; std::cout << "type of d: " << typeid(d).name() << '\n'; auto my_lambda = [](int x) { return x + 3; }; std::cout << "my_lambda: " << my_lambda(5) << '\n'; auto my_fun = get_fun(2); std::cout << "type of my_fun: " << typeid(my_fun).name() << '\n'; std::cout << "my_fun: " << my_fun(3) << '\n'; }
Output:
type of a: int type of b: double type of d: std::initializer_list<int> my_lambda: 8 type of my_fun: double (*)(double) my_fun: 0.14112