c++ - T 是否必须是完整类型才能在 `std::declval<T>` 中使用?

标签 c++ language-lawyer incomplete-type declval

考虑这个例子(来自 here ):

#include <type_traits>
#include <iostream>
template <typename U>
struct A {
};

struct B {
   template <typename F = int>
   A<F> f() { return A<F>{}; }

   using default_return_type = decltype(std::declval<B>().f());
};

int main()
{
    B::default_return_type x{};
    std::cout << std::is_same< B::default_return_type, A<int>>::value;
}

compiles with no errors on gcc9.2但 gcc7.2 和 clang 10.0.0 提示 B 不完整。铿锵错误是:

prog.cc:11:58: error: member access into incomplete type 'B'
   using default_return_type = decltype(std::declval<B>().f());
                                                         ^
prog.cc:7:8: note: definition of 'B' is not complete until the closing '}'
struct B {
       ^
prog.cc:16:8: error: no type named 'default_return_type' in 'B'
    B::default_return_type x{};
    ~~~^
prog.cc:17:35: error: no member named 'default_return_type' in 'B'
    std::cout << std::is_same< B::default_return_type, A<int>>::value;
                               ~~~^

最佳答案

错误的根源不是 std::declval,而是不完整的类成员访问。

直到解决CWG1836 was merged 2.5 年前,标准要求类在类成员访问表达式 (E1.E2) 中完整。
[expr.ref]/2 in C++11 :

For the first option (dot) the first expression shall have complete class type.

[expr.ref]/2 in C++17 :

For the first option (dot) the first expression shall be a glvalue having complete class type.

并且一个类在 alias-declaration 中不被认为是完整的。在自己的范围内member-specification .
[class.mem]/6 in C++17 :

A class is considered a completely-defined object type ([basic.types]) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments, noexcept-specifiers, and default member initializers (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification.

关于c++ - T 是否必须是完整类型才能在 `std::declval<T>` 中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59268030/

相关文章:

java - 兼容的 Java 实现可以不包含所有标准库吗?

c++ - if-else 取决于 T 是否为完整类型

c++ - 为什么 `true` 和 `false` 都被认为是变量?

c++ - 使用 SOLVEPNP_EPNP 的相机姿态估计是否对异常值敏感,可以纠正吗?

c++ - 我们不能在未评估的上下文中命名非静态成员函数有什么原因吗?

c++ - 不完整类型的 std::unique_ptr 无法编译

C++ 系统文件 bits/stat.h 突然中断, "error: field ‘st_atim’ 类型不完整”

c# - 编码 LPSTR 和 float 时堆栈不平衡

c++ - 返回指针的最佳方式

c++ - 为什么 GCC 对这些片段的处理方式不同?