std::declval
De 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 en la cabecera <utility>
|
||
| template< class T > typename std::add_rvalue_reference<T>::type declval(); |
(desde C++11) | |
Convierte cualquier
T tipo a un tipo de referencia, por lo que es posible el uso de funciones miembros en expresiones decltype sin especificar constructores. Es comúnmente usado en las plantillas donde los parámetros aceptables plantilla puede haber ningún constructor en común, pero tienen la misma función miembro cuyo tipo de retorno es necesario. std::declval sólo puede utilizarse en contextos no evaluadas, se trata de un error para evaluar una expresión que contiene esta función .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.
Contenido |
[editar] Parámetros
(Ninguno)
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
No puede ser llamado, por lo tanto nunca devuelve un valor, pero es el tipo de retorno
T&& menos T es un tipo de referencia lvalue, en cuyo caso se devuelve T& .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] Excepciones
[editar] Ejemplo
#include <utility> #include <iostream> struct Default { int foo() const {return 1;} }; struct NonDefault { NonDefault(const NonDefault&) {} int foo() const {return 1;} }; 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 n2 std::cout << "n2 = " << n2 << '\n'; }
Salida:
n2 = 1
[editar] Ver también
| decltype especificador | define un tipo equivalente al tipo de un (C++11) expresión
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) |
deduce el tipo de retorno de una expresión de llamada a la función 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. (plantilla de clase) |

