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

  • SEARCH
  • Home
  • 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 522507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:21:41+00:00 2026-05-13T08:21:41+00:00

Suppose I have a class like class Empty{ Empty(int a){ cout << a; }

  • 0

Suppose I have a class like

class Empty{
    Empty(int a){ cout << a; }
}

And then I invoke it using

int main(){
    Empty(2);
    return 0;
}

Will this cause any memory to be allocated on the stack for the creation of an “Empty” object? Obviously, the arguments need to be pushed onto the stack, but I don’t want to incur any extra overhead. Basically I am using the constructor as a static member.

The reason I want to do this is because of templates. The actual code looks like

template <int which>
class FuncName{
    template <class T>
    FuncName(const T &value){
        if(which == 1){
            // specific behavior
        }else if(which == 2){
            // other specific behavior
        }
    }
};

which allows me to write something like

int main(){
    int a = 1;
    FuncName<1>(a);
}

so that I get to specialize one template parameter, while not having to specify the type of T. Also, I am hoping the compiler will optimize the other branches away inside the constructor. If anyone knows if this is true or how to check, that would be greatly appreciated. I assumed also that throwing templates into the situation does not change the “empty class” problem from above, is that right?

  • 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-05-13T08:21:41+00:00Added an answer on May 13, 2026 at 8:21 am

    Quoting Stroustrup:

    Why is the size of an empty class not zero?
    To ensure that the addresses of two different objects will be different. For the same reason, “new” always returns pointers to distinct objects. Consider:

    class Empty { };
    
    void f()
    {
        Empty a, b;
        if (&a == &b) cout << "impossible: report error to compiler supplier";
    
        Empty* p1 = new Empty;
        Empty* p2 = new Empty;
        if (p1 == p2) cout << "impossible: report error to compiler supplier";
    }   
    

    There is an interesting rule that says that an empty base class need not be represented by a separate byte:

    struct X : Empty {
        int a;
        // ...
    };
    
    void f(X* p)
    {
        void* p1 = p;
        void* p2 = &p->a;
        if (p1 == p2) cout << "nice: good optimizer";
    }
    

    This optimization is safe and can be most useful. It allows a programmer to use empty classes to represent very simple concepts without overhead. Some current compilers provide this “empty base class optimization”.

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

Sidebar

Ask A Question

Stats

  • Questions 282k
  • Answers 282k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use psutil (https://github.com/giampaolo/psutil), which works on Windows and… May 13, 2026 at 4:00 pm
  • Editorial Team
    Editorial Team added an answer There are a few solutions out there, if you want… May 13, 2026 at 4:00 pm
  • Editorial Team
    Editorial Team added an answer This occurs because local values override style values. (Properties set… May 13, 2026 at 4:00 pm

Related Questions

I've been working on a code generater for my projects and, while all has
I have a hierarchy of objects that all implement INotifyPropertyChanged. I also have a
I've got a large set of inherited classes (criteria) which inherit from a base
Any efficient/reliable way to expose one event? I have a class, MultipleDocumentCopier that copies
I have a custom control derived from Button: class MyControl : Button{} And suppose,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.