std::declval
Da cppreference.com
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Definido no cabeçalho <utility> | ||
template<class T > typenamestd::add_rvalue_reference<T>::type declval(); | (desde C++11) | |
Converte qualquer tipo
T
a um tipo de referência, tornando possível usar funções de membro em expressões decltype sem especificar construtores. É comumente utilizado em modelos onde os parâmetros do modelo aceitáveis podem não têm um construtor em comum, mas têm a função mesmo membro cujo tipo de retorno é necessária. std::declval só pode ser utilizada em contextos não avaliada, é um erro para calcular uma expressão que contém esta função.Original:
Converts any type
T
to a reference type, making it possible to use member functions in decltype expressions without specifying constructors. It is commonly used in templates where acceptable template parameters may have no constructor in common, but have the same member function whose return type is needed. std::declval can only be used in unevaluated contexts, it is an error to evaluate an expression that contains this function.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.
Índice |
[editar]Parâmetros
(Nenhum)
Original:
(none)
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.
[editar]Valor de retorno
Não pode ser chamado, assim nunca retorna um valor, mas o tipo de retorno é
T&&
menos T
é um tipo de referência lvalue, em que T&
caso é devolvido.Original:
Cannot be called, thus never returns a value, but the return type is
T&&
unless T
is an lvalue reference type, in which case T&
is returned.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.
[editar]Exceções
[editar]Exemplo
#include <utility>#include <iostream> struct Default {int foo()const{return1;}}; struct NonDefault { NonDefault(const NonDefault&){}int foo()const{return1;}}; int main(){ decltype(Default().foo()) n1 =1;// int n1// decltype(NonDefault().foo()) n2 = n1; // will not compile decltype(std::declval<NonDefault>().foo()) n2 = n1;// int n2std::cout<<"n2 = "<< n2 <<'\n';}
Saída:
n2 = 1
[editar]Veja também
decltype especificador | define um tipo equivalente ao tipo de uma (C++11) expressão Original: defines a type equivalent to the type of an expression (C++11) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
(C++11) | deduz o tipo de retorno de uma expressão chamada de função Original: deduces the return type of a function call expression The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (modelo de classe) |