Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:54:16+00:00 2026-06-02T04:54:16+00:00

Given a template class like this: template <typename T> class C { T member;

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:54:18+00:00Added an answer on June 2, 2026 at 4:54 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a class with a member template function like this one: template <typename t>
Having trouble inheriting from a template class. Looks something like this: template<typename type> class
Given the following template: template <typename T> class wrapper : public T {}; What
Given the following piece of code: template<typename T> class MyContainer { typedef T value_type;
g++ compiler gives this error: expected `;' before 'it' template <typename T> class myList
Given: template<typename T> class A { B b; std::vector<T> vec1; std::vector<T> vec2; } I'd
I have created a class Person that looks like this: public class Person {
Given a template class as such: template <typename TYPE> class SomeClass { public: typedef
I have code like this: template <typename T, typename U> struct MyStruct { T
If a template class definition contains a static member variable that depends on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.