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. |
Déclaré dans l'en-tête <utility>
|
||
template< class T > typename std::add_rvalue_reference<T>::type declval(); |
(depuis C++11) | |
Convertit n'importe quel type de
T
à un type référence, ce qui permet d'utiliser les fonctions de membre dans les expressions decltype sans préciser les constructeurs. Il est couramment utilisé dans les modèles où les paramètres des modèles acceptables peuvent avoir aucun constructeur en commun, mais ils ont la même fonction membre dont le type de retour est nécessaire. std::declval ne peut être utilisé dans des contextes non évaluées, il s'agit d'une erreur d'évaluer une expression qui contient cette fonction .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.
Sommaire |
[modifier] Paramètres
(Aucun)
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.
[modifier] Retourne la valeur
Ne peut pas être appelé, ce qui ne retourne jamais de valeur, mais le type de retour est
T&&
moins T
est un type référence lvalue, dans lequel T&
affaire est renvoyée .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.
[modifier] Exceptions
[modifier] Exemple
#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'; }
Résultat :
n2 = 1
[modifier] Voir aussi
decltype spécificateur | définit un type équivalent au type d'une expression (C++11)
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) |
déduit le type de retour d'une expression d'appel de la fonction 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. (classe générique) |