std::declval
Aus 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. |
definiert in Header <utility> | ||
template<class T > typenamestd::add_rvalue_reference<T>::type declval(); | (seit C++11) | |
Konvertiert jede Art
T
zu einer Referenz-Typ, die es ermöglichen, Member-Funktionen in decltype Ausdrücken ohne Angabe Konstruktoren verwenden. Es wird allgemein in Vorlagen, wo akzeptable Template-Parameter keinen Konstruktor gemeinsam haben können, aber die gleiche Member-Funktion, deren Rückgabetyp erforderlich ist. std::declval nur in unevaluierten Kontexten verwendet werden, ist es ein Fehler, um einen Ausdruck enthält, die diese Funktion auszuwerten .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.
Inhaltsverzeichnis |
[Bearbeiten]Parameter
(None)
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.
[Bearbeiten]Rückgabewert
Kann nicht aufgerufen werden, also nie einen Wert zurückgibt, aber die Rückkehr Typ ist
T&&
es sei denn, T
ist ein L-Wert Referenz-Typ, in welchem Fall T&
zurückgegeben .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.
[Bearbeiten]Ausnahmen
[Bearbeiten]Beispiel
#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';}
Output:
n2 = 1
[Bearbeiten]Siehe auch
decltype Spezifizierer | definiert eine Art entspricht dem Typ eines Ausdrucks (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) | leitet den Rückgabetyp einer Funktion Anruf Ausdruck 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. (Klassen-Template) |