I would like to craft an "interface"/mix-in class (template), and ensure nobody thinks that adding a member to this class template is a good idea, I'd like to static_assert
on this condition.
Unfortunately, std::is_empty
does not allow virtual functions (as polymorphic classes need some space to store their virtual function table pointer or equivalent implementation details).
Is there a variant of std::is_empty
that allows for virtual functions, but not data members (or is it easily written)?
I'd like this to work (plus all dark corner cases I can't think of right now):
#include <type_traits>
struct A {};
struct B : A {};
struct AA { virtual ~AA() = default; };
struct BB : AA {};
static_assert(std::is_empty_v<A>);
static_assert(std::is_empty_v<B>);
// These should also work with the replacement for is_empty:
static_assert(std::is_empty_v<AA>);
static_assert(std::is_empty_v<BB>);
int main()
{}
I'm guessing it requires compiler magic or very platform-specific hacks/checks depending on how exactly virtual functions are implemented.