Namespaces
Variants

std:: declval

From cppreference.net
Utilities library
定义于头文件 <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)
推导可调用对象与一组参数调用的结果类型
(类模板)