std::declval

来自cppreference.com
< cpp‎ | utility
 
 
 
在标头 <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 不允许出现于求值语境");
}

[编辑] 示例

#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)(C++20 移除)(C++17)
推导以一组实参调用一个可调用对象的结果类型
(类模板) [编辑]