Xem xét ví dụ này (đến từ đây ):
#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;
}
Nó biên dịch không có lỗi trên gcc9.2 nhưng gcc7.2 và clang 10.0.0 phàn nàn về việc Bkhông hoàn thành. Lỗi Clangs là:
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::declvalnó thì không còn quan trọng nữa nếu kiểu đó đã hoàn thành hay chưa (và tôi đoán là tôi đã sai với điều đó)
Bkhông hoàn thành cũng không được coi là hoàn thành alias-declaration.
.f(). Điều đó có ý nghĩa; loại không đầy đủBkhông có thành viênf.