Is there a way to static_assert that a type T is Not complete at that point in a header? The idea is to have a compile error if someone adds #includes down
Passing a reference through ...
doesn't work.
5.2.2/7:
When there is no parameter for a given argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by invoking
va_arg
(18.10). [note skipped — n.m.] The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed on the argument expression. An argument that has (possiblycv
-qualified) typestd::nullptr_t
is converted to typevoid*
(4.10). After these conversions, if the argument does not have arithmetic, enumeration, pointer, pointer to member, or class type, the program is ill-formed.
Here's a kind-of-working solution hastily adapted from @chris's comment:
#include
#include
namespace
{
template
constexpr auto is_complete(int) -> decltype(sizeof(T),bool{}) {
return true;
}
template
constexpr auto is_complete(...) -> bool {
return false;
}
}
#define IS_COMPLETE(T) is_complete(0) // or use __COUNTER__ if supported
struct S;
static_assert(IS_COMPLETE(int), "oops 1!");
static_assert(!IS_COMPLETE(S), "oops 2!");
struct S {};
static_assert(IS_COMPLETE(S), "oops 3!");