Does T have to be a complete type to be used in `std::declval<T>`?
The source of the error is not std::declval
, but incomplete class member access.
Until the resolution of CWG1836 was merged 2.5 years ago, the standard required the class to be complete in a class member access expression (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.
And a class is not regarded as complete in 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.
From [declval]:
Remarks: The template parameter
T
ofdeclval
may be an incomplete type.
This wording has been present since C++11 (so it's not possible for compilers to be conforming to an earlier standard)