std::declval

来自cppreference.com

 
 
实用工具库
类型的支持 (basic types, RTTI, type traits)
动态内存管理
错误处理
程序实用工具
可变参数函数
日期和时间
函数对象
initializer_list(C++11)
bitset
hash(C++11)
关系运算符
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
双和元组
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
掉期,远期和移动
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
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.

目录

[编辑] 参数

(无)
Original:
(none)
The text has been machine-translated via Google Translate.
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.

[编辑] 例外

noexcept specification:  
noexcept
  (C++11 起)

[编辑] 为例

#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.
[edit]
(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.

(类模板) [edit]