◐ Shell
clean mode source ↗

decltype specifier - cppreference.com

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.

Click here for the English version of this page

<metanoindex/>

Ispeziona il tipo dichiarato di un'entità o interroga il tipo restituito di un'espressione.

Original:

Inspects the declared type of an entity or queries the return type of an expression.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Sintassi

decltype ( entity ) (1) (dal C++11)
decltype ( expression ) (2) (dal C++11)

Spiegazione

1)

Se l'argomento è il nome unparenthesised di un oggetto / funzione, o è un'espressione accesso ai membri (o object.member pointer->member), allora il decltype specifica il tipo dichiarato della entity specificato con questa espressione.

Original:

If the argument is either the unparenthesised name of an object/function, or is a member access expression (object.member or pointer->member), then the decltype specifies the declared type of the entity specified by this expression.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

2)

Se l'argomento è qualsiasi altra espressione di T tipo, allora

Original:

If the argument is any other expression of type T, then

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

a)

se il valore della categoria di expression è xValue', poi la decltype specifica T&&

Original:

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

b)

se il valore di categoria expression è lvalue', poi la decltype specifica T&

Original:

if the value category of expression is lvalue, then the decltype specifies T&

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

c)

in caso contrario, decltype specifica T

Original:

otherwise, decltype specifies T

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Si noti che se il nome di un oggetto è tra parentesi tonde, diventa espressione lvalue, così decltype(arg) e decltype((arg)) sono spesso diversi tipi.

Original:

Note that if the name of an object is parenthesised, it becomes an lvalue expression, thus decltype(arg) and decltype((arg)) are often different types.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

decltype è utile quando si dichiara tipi che sono difficili o impossibili da dichiarare usando la notazione standard, come lambda relativi tipi o tipi che dipendono da parametri di modello.

Original:

decltype is useful when declaring types that are difficult or impossible to declare using standard notation, like lambda-related types or types that depend on template parameters.

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Parole chiave

decltype

Esempio

#include <iostream>

struct A {
   double x;
};
const A* a = new A();

decltype( a->x ) x3;       // type of x3 is double (declared type)
decltype((a->x)) x4 = x3;  // type of x4 is const double& (lvalue expression)

template <class T, class U>
auto add(T t, U u) -> decltype(t + u); // return type depends on template parameters

int main() 
{
    int i = 33;
    decltype(i) j = i*2;
   
    std::cout << "i = " << i << ", "
              << "j = " << j << '\n';

    auto f = [](int a, int b) -> int {
       return a*b;
    };
   
    decltype(f) f2{f}; // the type of a lambda function is unique and unnamed
    i = f(2, 2);
    j = f2(3, 3);
   
    std::cout << "i = " << i << ", "
              << "j = " << j << '\n';
}

Output:

i = 33, j = 66
i = 4, j = 9

Vedi anche

ottiene il tipo di espressione in un contesto non valutata

Original:

obtains the type of expression in unevaluated context

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(funzione di modello) [modifica]