Consider this example (coming from 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;
}
It compiles with no errors on gcc9.2 but gcc7.2 and clang 10.0.0 complain about B
not being complete. Clangs error is:
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;
~~~^
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:
[expr.ref]/2 in C++17:
And a class is not regarded as complete in
alias-declaration
within its ownmember-specification
.[class.mem]/6 in C++17: