std::declval
来自 zh.cppreference.net
| 定义于头文件 <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; // n1 的类型是 int decltype(std::declval<Default>().foo()) n2 = 1; // 同上 // decltype(NonDefault().foo()) n3 = n1; // 错误:无默认构造函数 decltype(std::declval<NonDefault>().foo()) n3 = n1; // n3 的类型是 int std::cout << "n1 = " << n1 << '\n' << "n2 = " << n2 << '\n' << "n3 = " << n3 << '\n'; }
输出:
n1 = 1 n2 = 1 n3 = 1
参见
decltype
说明符
(C++11)
|
获取表达式或实体的类型 |
|
(C++11)
(removed in C++20)
(C++17)
|
推导可调用对象与一组参数调用的结果类型
(类模板) |