Skip to content

Commit 2e3d694

Browse files
authored
[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.
1 parent 1d56c50 commit 2e3d694

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,6 +2476,16 @@ bool hasNItemsOrLess(ContainerTy &&C, unsigned N) {
24762476
template <class Ptr> auto to_address(const Ptr &P) { return P.operator->(); }
24772477
template <class T> constexpr T *to_address(T *P) { return P; }
24782478

2479+
// Detect incomplete types, relying on the fact that their size is unknown.
2480+
namespace detail {
2481+
template <typename T> using has_sizeof = decltype(sizeof(T));
2482+
} // namespace detail
2483+
2484+
/// Detects when type `T` is incomplete. This is true for forward declarations
2485+
/// and false for types with a full definition.
2486+
template <typename T>
2487+
constexpr bool is_incomplete_v = !is_detected<detail::has_sizeof, T>::value;
2488+
24792489
} // end namespace llvm
24802490

24812491
namespace std {

llvm/unittests/ADT/STLExtrasTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,4 +1300,10 @@ TEST(STLExtrasTest, LessSecond) {
13001300
}
13011301
}
13021302

1303+
struct Foo;
1304+
struct Bar {};
1305+
1306+
static_assert(is_incomplete_v<Foo>, "Foo is incomplete");
1307+
static_assert(!is_incomplete_v<Bar>, "Bar is defined");
1308+
13031309
} // namespace

0 commit comments

Comments
 (0)