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. |
<metanoindex/>
<tbody> </tbody>| Dclar dans l'en-tte <utility>
|
||
template< class T > typename std::add_rvalue_reference<T>::type declval(); |
(depuis C++11) | |
Convertit n'importe quel type de
T un type rfrence, ce qui permet d'utiliser les fonctions de membre dans les expressions decltype sans prciser les constructeurs. Il est couramment utilis dans les modles o les paramtres des modles acceptables peuvent avoir aucun constructeur en commun, mais ils ont la mme fonction membre dont le type de retour est ncessaire. std::declval ne peut tre utilis dans des contextes non values, 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.
Paramtres
(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.
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 rfrence lvalue, dans lequel T& affaire est renvoye .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.
Exceptions
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';
}
Rsultat :
n2 = 1
Voir aussi
| decltype spcificateur | dfinit 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) |
dduit 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 gnrique) |