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

Does T have to be a complete type to be used in `std::declval<T>`?

考虑这个例子(来自):

#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 不完整。 Clangs 错误是:

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;
                               ~~~^

来自[declval]

Remarks: The template parameter T of declval may be an incomplete type.

这种措辞自 C++11 以来就存在(因此编译器不可能符合更早的标准)

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

在 2.5 年前的 CWG1836 was merged 决议之前,标准要求 class 在 class 成员访问表达式 (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.

而一个class在alias-declaration within its own 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.