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?
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:
should work, I think. But you might trip over the fact that
C<void>::memberdoesn’t exist, so any member functions that use it won’t compile forC<void>, including constructors.Why not have
Ca non-template class with no additional data member, and if the user wants to add data members, useCas a base class? Remember to giveCa protected destructor — every base class should have either a protected destructor or a virtual destructor, but since yourCtemplate 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:
and if you really need it, add a type trait:
Then where you would have written
C<MyAdditionalData>, now you writeCWithData<MyAdditionalData>, and where you would have writtenC<some_type_that_might_be_void>you writetypename C<some_type_that_might_be_void>::type.