◐ Shell
clean mode source ↗

decltype specifier - cppreference.com

De cppreference.com

<metanoindex/>

Inspeciona o tipo declarado de uma entidade ou consulta o tipo de retorno de uma expressão.

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.

Sintaxe

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

Explicação

1)

Se o argumento é o nome unparenthesised de um objeto / função, ou é uma expressão de acesso a membros (object.member ou pointer->member), então o decltype especifica o tipo declarado do entity especificado por esta expressão.

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 o argumento for qualquer outra expressão de T tipo, então

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 o categoria valor de expression is' xValue, então o decltype especifica T&&

Original:

if the categoria valor of expression is xvalue, 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.

b)

se o valor da categoria expression is' lvalue, então o decltype especifica 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)

caso contrário, decltype especifica 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.

Note que, se o nome de um objeto é parenthesised, torna-se uma expressão lvalue, assim decltype(arg) e decltype((arg)) são muitas vezes diferentes tipos.

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 é útil quando declarar tipos que são difíceis ou impossíveis de se declarar usando a notação padrão, tal como a lambda-relacionados tipos ou tipos que dependem de parâmetros de modelo.

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.

Palavras-chave

decltype

Exemplo

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

Saída:

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

Veja também

obtém o tipo de expressão em contexto não avaliada

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.


(modelo de função) [edit]