std::declval
来自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. |
| Defined in header <utility>
|
||
| template< class T > typename std::add_rvalue_reference<T>::type declval(); |
(C++11 起) | |
任何类型
T转换为引用类型,使得它可以使用decltype表达式中没有指定构造函数的成员函数。它通常用于在可接受的模板参数可能没有构造函数中常见的模板,但具有相同的成员函数的返回类型是必要的。 std::declval只能用于非计算环境下,这是一个错误,来评估一个表达式,其中包含此功能.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.
目录 |
[编辑] 参数
(无)
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.
[编辑] 返回值
不会被调用,因此永远不会返回一个值,但返回类型是
T&&除非T是一个左值的引用类型,在这种情况下,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.
[编辑] 例外
[编辑] 为例
#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'; }
Output:
n2 = 1
[编辑] 另请参阅
| decltype说明 | 定义了一个类型相当于一个表达式(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) |
推导出的返回类型的函数调用表达式 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. (类模板) |