Is there an easy way to tell if a class/struct has no data members?

前端 未结 4 875
后悔当初
后悔当初 2020-12-07 00:20

Hallo,

is there some easy way in C++ to tell (in compile-time) if a class/struct has no data members?

E.g. struct T{};

My first thought

4条回答
  •  心在旅途
    2020-12-07 01:10

    Stepping on Konrad's answer, this handles classes with or without virtual functions.

    template 
    struct is_empty {
        struct empty_ { virtual ~empty_(); };
        struct helper_ : T { virtual ~helper_(); };
        static bool const EMPTY = sizeof(helper_) == sizeof(empty_);
    };
    

提交回复
热议问题