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?

Answers


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.


Specialize your template class for a type NoData and don't define the member in this specialization.

template <class T>
class C {
   T member;
...
};

template<>
class C<NoData> {
   // Do not declare that member
};

Ok I found that an empty class is the closest I can get 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?

No. You can define one yourself:

struct standard_empty_class {};

Use it.

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.

I don't understand this. If the user doesn't want to store any data, then why even then you need a class, and still need to write C<NoData>? Just forget that case. No data means no class. Does it not solve that?


I don't think it's possible. Actually, it's the other way arround. Even an empty class occupies memory (one byte) when instantiated, so that the program can differ between instances. That is

class A {
};

A a, b;  // vars of type A

Then a and b must take some memory so your program can refer to them, for example, if (a == b). You can't do it if they don't use memory.


As you've said, the template already contains some other data. It it has virtual methods, then that's one more member.

Adding an extra member is a very small overhead that can be simply ignored, so I'd just use C. (or even char if there are other members smaller than alignment)

Use typedef for better clarity:

typedef C<int> CEmptyGraphNode;

Need Your Help

How to make a vTiger crm custom module can only display for administrator

php smarty vtiger

I am trying to develop a custom module in vTiger crm, I have a requirement that the custom module can only display for Administrator.

C# , wsdl proxy classes , how to use it?

c# service proxy wsdl

I have a wsdl and xsd files, and with wsdl.exe utility i generate proxy classes, but in project i have not any service reference, haow can use it with c#?