[ADT] Add detection utility for incomplete types (#65495)

This allows us to produce better error messages for types that were only
forward-declared, but where a full definition was expected.

The first user will be https://reviews.llvm.org/D159013; this change is
sent to review separately to reduce the scope of the other patch.
This commit is contained in:
Jakub Kuderski 2023-09-06 14:06:40 -04:00 committed by GitHub
parent 1d56c509be
commit 2e3d694018
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -2476,6 +2476,16 @@ bool hasNItemsOrLess(ContainerTy &&C, unsigned N) {
template <class Ptr> auto to_address(const Ptr &P) { return P.operator->(); }
template <class T> constexpr T *to_address(T *P) { return P; }
// Detect incomplete types, relying on the fact that their size is unknown.
namespace detail {
template <typename T> using has_sizeof = decltype(sizeof(T));
} // namespace detail
/// Detects when type `T` is incomplete. This is true for forward declarations
/// and false for types with a full definition.
template <typename T>
constexpr bool is_incomplete_v = !is_detected<detail::has_sizeof, T>::value;
} // end namespace llvm
namespace std {

View File

@ -1300,4 +1300,10 @@ TEST(STLExtrasTest, LessSecond) {
}
}
struct Foo;
struct Bar {};
static_assert(is_incomplete_v<Foo>, "Foo is incomplete");
static_assert(!is_incomplete_v<Bar>, "Bar is defined");
} // namespace