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) |
推导调用可调用对象与一组参数的结果类型 (类模板) |