Optional base class template to get conditional data members In generic code, I sometimes want to conditionally add a data member to a class template. Writing separate template specializations scales as 2^N for N conditional data members. Absent a static_if feature, I have found the following user-defined class templates empty_base&lt;T&gt; and optional_base&lt;T&gt; useful:

namespace xstd {
namespace block_adl {

template&lt;class&gt;
struct empty_base
{
        empty_base() = default;

        template&lt;class... Args&gt;
        constexpr empty_base(Args&amp;&amp;...) {}
};

template&lt;bool Condition, class T&gt;
using optional_base = std::conditional_t&lt;Condition, T, empty_base&lt;T&gt;&gt;;

}       // namespace block_adl

// deriving from empty_base or optional_base will not make xstd an associated namespace
// this prevents ADL from finding overloads for e.g. begin/end/swap from namespace xstd
using block_adl::empty_base;
using block_adl::optional_base;

}       // namespace xstd


Exploiting the empty base optimization then allows users to write class templates that have optional data members (conditional on user-defined template variable traits Trait1_v and Trait2_v)

template&lt;class T&gt;
class Test
:
    public Base0, // unconditional base class
    public xstd::optional_base&lt;Trait1_v&lt;T&gt;, Base1&gt;,
    public xstd::optional_base&lt;Trait2_v&lt;T&gt;, Base2&gt;
{
    using B1 = xstd::optional_base&lt;Trait1_v&lt;T&gt;, Base1&gt;;
    using B2 = xstd::optional_base&lt;Trait2_v&lt;T&gt;, Base2&gt;;
public:
    Test() = default;

    Test(Arg0 const&amp; a0, Arg1 const&amp; a1, Arg2 const&amp; a2)
    :
        Base0(a0),
        B1(a1),
        B2(a2)
    {}    
};


Note that the optional_base variadic constructor allows linear scaling of constructor delegation in the user-defined class template Test. One minor wart is that this constructor does require passing all parameters (but otherwise, 2^N different constructors have to be written). But if any of the traits variables evaluates to false, the corresponding parameter and call to the base constructor will be optimized away. And because optional_base also has a trivial default constructor, POD-ness of user-defined classes is preserved by inheriting from it. 

Obviously, over-use of inheritance has to be avoided, but the above design does avoid quite a lot of code duplication. Oh, and I haven't tested this with virtual functions, virtual base classes and what-not. I just use this for assembling value-semantic classes out of conditional building blocks.

Question: what other design / usability issues could there be with the above code?
I only have a few comments. First, you probably want to inherit your bases privately:

template&lt;class T&gt;
class Test
:
    private Base0, // unconditional base class
    private xstd::optional_base&lt;Trait1_v&lt;T&gt;, Base1&gt;,
    private xstd::optional_base&lt;Trait2_v&lt;T&gt;, Base2&gt;
{ };


After all, your design is about adding data members. That's something that you probably don't want to expose to the outside world. 

Secondly, what happens if you want to potentially have multiple different members of the same type? That's definitely going to come up and so should be supported. In order to do that, you might want to add some kind of ID to differentiate them and then additionally wrap the non-empty case. That is:

template &lt;class, int &gt;
struct empty_base { ... }; // same as before

template &lt;class T, int &gt;
struct wrapped {
    T val;
};  

template &lt;bool Condition, class T, int UniqueID = 0&gt;
using optional_base = std::conditional_t&lt;Condition,
                                         wrapped&lt;T, UniqueID&gt;,
                                         empty_base&lt;T, UniqueID&gt;&gt;;


This will additionally let you use non-class types as optional data members (since you couldn't inherit from, e.g. int). So you can now do something like:

template &lt;class T&gt;
class Test
: private xstd::optional_base&lt;Trait1_v&lt;T&gt;, int, 0&gt;
, private xstd::optional_base&lt;Trait2_v&lt;T&gt;, int, 1&gt;
{ ... };


That adds an inconvenience that you have to explicitly add the sequence 0, 1, ...



You can get around that by making a linear inheritance list of all your optional bases. I'm not sure if it's worth it, but I'll present the idea just as an option. At its core, we have OptionalBasesImpl which will inherit from all the optional bases and provide a getter so that the derived class can actually use them:

template &lt;typename... &gt;
struct typelist { };

template &lt;int, typename... &gt;
struct OptionalBasesImpl;


The root case is empty:

template &lt;int ID&gt;
struct OptionalBasesImpl&lt;ID&gt;
{
    OptionalBasesImpl() = default;
    template &lt;typename... Args&gt;
    OptionalBasesImpl(Args&amp;&amp;... ) { }

    void get(typelist&lt;&gt; ) { }
};


And we recurse over each pair of type trait/type:

template &lt;int ID, typename Cond, typename T, typename... Rest&gt;
struct OptionalBasesImpl&lt;ID, Cond, T, Rest...&gt;
: private xstd::optional_base&lt;Cond::value, T, ID&gt;
, OptionalBasesImpl&lt;ID + 1, Rest...&gt;
{
    using Base = xstd::optional_base&lt;Cond::value, T, ID&gt;;
    using Root = OptionalBasesImpl&lt;ID + 1, Rest...&gt;;

    OptionalBasesImpl() = default;

    template &lt;typename Arg, typename... Args&gt;
    OptionalBasesImpl(Arg&amp;&amp; a0, Args&amp;&amp;... args)
    : Base{std::forward&lt;Arg&gt;(a0)}
    , Root(std::forward&lt;Args&gt;(args)...)
    { }

protected:
    using Root::get;

    // by conditional/type pair
    Base&amp; get(typelist&lt;Cond, T&gt; ) { return *this; }
    Base const&amp; get(typelist&lt;Cond, T&gt; ) const { return *this; }

    // by index
    Base&amp; get(std::integral_constant&lt;int, ID&gt; ) { return *this; } 
    Base const&amp; get(std::integral_constant&lt;int, ID&gt; ) const { return *this; }
};


Now we just have the top-level class to start us off at 0:

template &lt;typename... T&gt;
struct OptionalBases : OptionalBasesImpl&lt;0, T...&gt; {
protected:
    using OptionalBasesImpl&lt;0, T...&gt;::OptionalBasesImpl;
    using OptionalBasesImpl&lt;0, T...&gt;::get;
};


which lets you write something like:

template &lt;typename T&gt;
struct Test
: private OptionalBases&lt;std::is_integral&lt;T&gt;, int,
                        std::is_same&lt;T, int&gt;, int&gt;
{
    using Bases = OptionalBases&lt;std::is_integral&lt;T&gt;, int,
                        std::is_same&lt;T, int&gt;, int&gt;;
    using Bases::Bases;

    void printFirst() {
        std::cout &lt;&lt; Bases::get(typelist&lt;std::is_integral&lt;T&gt;, int&gt;{}).val &lt;&lt; std::endl;
    }

    void printSecond() {
        std::cout &lt;&lt; Bases::get(std::integral_constant&lt;int, 1&gt;{}).val &lt;&lt; std::endl;
    }
};

int main() {
    Test&lt;int&gt; t{1, 2};
    t.printFirst();
    t.printSecond();
}


That's a lot of extra boilerplate for just avoiding writing a sequence of integers, so YMMV.