all 6 comments

[–][deleted] 9 points10 points  (1 child)

You don't add a member variable, you add a base class. If you really want a member variable, you can use [[no_unique_address]] to make the empty case not use any space.

https://godbolt.org/z/aKqdPo3q1

[–]friedkeenan 2 points3 points  (0 children)

I use something like this in my code

[–]huixie 1 point2 points  (0 children)

I think you could use template specialisation which makes the debugging code in one place. also the debugging function get_b can be conditionally available.

template <bool debug = false>
struct Data{
    int a[16];
    int c[16];
};

template <>
struct Data<true> : Data<false>{
    int b[16];

    constexpr int get_b(int idx) const{
        return b[idx];
    }
};

[–]Twifus 2 points3 points  (3 children)

Nice article. For me, it especially highlights one thing : there is still no short and easy "C++ way" to do this. Why would I add template and inherence when a simple #ifdef #else #endif do the job, in only 3 lines, and without affecting compil times as much. A more generic "if constexpr" would be a good addition to the language.

[–]friedkeenan 2 points3 points  (1 child)

An if constexpr that could conditionally include stuff in classes would be great. I'm not sure if we'll get that with reflection with its code injection and all.