std::declval
来自 cppreference.cn
定义于头文件 <utility> |
||
template< class T > typename std::add_rvalue_reference<T>::type declval() noexcept; |
(自 C++11 起) (直到 C++14) (仅非求值语境) |
|
template< class T > std::add_rvalue_reference_t<T> declval() noexcept; |
(自 C++14 起) (仅非求值语境) |
|
用于编写出现在非求值语境(通常为 decltype
的操作数)中的表达式的辅助模板。在非求值语境中,此辅助模板将任何类型 T
(可以是未完成类型) 转换为该类型的表达式,从而可以无需通过构造函数即可使用 T 的成员函数。
std::declval
只能在非求值语境中使用,并且不需要定义;若求值包含此函数的表达式则会出错。正式地,如果此函数被ODR 使用,则程序是非良构的。
目录 |
[编辑] 参数
(无)
[编辑] 返回值
无法求值,因此永不返回值。返回类型为 T&&
(引用折叠规则适用),除非 T
是 (可能带 cv 限定的) void,在这种情况下,返回类型为 T
。
[编辑] 注解
std::declval
常用于模板,在模板中,可接受的模板参数可能没有共同的构造函数,但具有相同的成员函数,而后者需要其返回类型。
[编辑] 可能的实现
template<typename T> typename std::add_rvalue_reference<T>::type declval() noexcept { static_assert(false, "declval not allowed in an evaluated context"); } |
[编辑] 示例
运行此代码
#include <iostream> #include <utility> struct Default { int foo() const { return 1; } }; struct NonDefault { NonDefault() = delete; int foo() const { return 1; } }; int main() { decltype(Default().foo()) n1 = 1; // type of n1 is int decltype(std::declval<Default>().foo()) n2 = 1; // same // decltype(NonDefault().foo()) n3 = n1; // error: no default constructor decltype(std::declval<NonDefault>().foo()) n3 = n1; // type of n3 is int std::cout << "n1 = " << n1 << '\n' << "n2 = " << n2 << '\n' << "n3 = " << n3 << '\n'; }
输出
n1 = 1 n2 = 1 n3 = 1
[编辑] 参见
decltype 说明符(C++11) |
获取表达式或实体的类型 |
(C++11)(在 C++20 中移除)(C++17) |
推导以一组参数调用可调用对象的结果类型 (类模板) |