Search code examples
c++classtemplatesmember

Is there a possibility to declare a member variable that takes up no memory?


Given a template class like this:

template <typename T>
class C
{
T member;
//... some other members that are not of interest here
};

Is there any type I can give as T to prevent member from taking up unnecessary memory? At first void popped to my mind but I know that you cannot declare variables of void.

NOTE
Of course the example is simplified. The background is a class that holds some information but can get additional information added by the user. When the user does not want to add additional information it should be possible to leave them out. So basically if the user wants to store additional data he will construct C<MyAdditionalData> but if he does not it should be like C<NoData> and there will be no data. Of course i could write some kind of template specialization but I don't like writing everything twice.

EDIT
Ok I found that an empty class is the closest I can get (still consumes 1 byte or even 4/8 because of alignment) so far, so my question now is: Is there already some standard empty class that I should be using for this to make my code more readable?


Solution

  • With a data member no, because objects are never size zero, and there's no get-out clause for data members.

    However, a base class subobject can have size zero if it's empty. Hence:

    template <typename T>
    struct Member {
        T member;
    };
    
    template <>
    struct Member<void> {
    };
    
    template <typename T>
    class C : private Member<T>
    {
    //... some other members that are not of interest here
    };
    

    should work, I think. But you might trip over the fact that C<void>::member doesn't exist, so any member functions that use it won't compile for C<void>, including constructors.

    if the user wants to store additional data he will construct C<MyAdditionalData>

    Why not have C a non-template class with no additional data member, and if the user wants to add data members, use C as a base class? Remember to give C a protected destructor -- every base class should have either a protected destructor or a virtual destructor, but since your C template class doesn't have a virtual destructor then there's no need for the non-template version to be virtual either.

    Along these lines, you could do:

    class CNoData
    {
    //... some other members that are not of interest here
    };
    
    template <typename T>
    class CWithData<T> : CNoData
    {
    T member
    // have to duplicate constructors, unfortunately.
    };
    

    and if you really need it, add a type trait:

    template <typename T>
    struct C {
         typedef CWithData<T> type;
    };
    
    template <>
    struct C<void> {
        typedef CNoData type;
    };
    

    Then where you would have written C<MyAdditionalData>, now you write CWithData<MyAdditionalData>, and where you would have written C<some_type_that_might_be_void> you write typename C<some_type_that_might_be_void>::type.