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 6876761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:31:05+00:00 2026-05-27T04:31:05+00:00

Suppose I have the following: int main() { SomeClass(); return 0; } Without optimization,

  • 0

Suppose I have the following:

int main() {
    SomeClass();
    return 0;
}

Without optimization, the SomeClass() constructor will be called, and then its destructor will be called, and the object will be no more.

However, according to an IRC channel that constructor/destructor call may be optimized away if the compiler thinks there’s no side effect to the SomeClass constructors/destructors.

I suppose the obvious way to go about this is not to use some constructor/destructor function (e.g use a function, or a static method or so), but is there a way to ensure the calling of the constructors/destructors?

  • 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-27T04:31:05+00:00Added an answer on May 27, 2026 at 4:31 am

    However, according to an IRC channel that constructor/destructor call may be optimized away if the compiler thinks there’s no side effect to the SomeClass constructors/destructors.

    The bolded part is wrong. That should be: knows there is no observable behaviour

    E.g. from § 1.9 of the latest standard (there are more relevant quotes):

    A conforming implementation executing a well-formed program shall produce the same observable behavior
    as one of the possible executions of the corresponding instance of the abstract machine with the same program
    and the same input. However, if any such execution contains an undefined operation, this International
    Standard places no requirement on the implementation executing that program with that input (not even
    with regard to operations preceding the first undefined operation).

    As a matter of fact, this whole mechanism underpins the sinlge most ubiquitous C++ language idiom: Resource Acquisition Is Initialization

    Backgrounder

    Having the compiler optimize away the trivial case-constructors is extremely helpful. It is what allows iterators to compile down to exactly the same performance code as using raw pointer/indexers.

    It is also what allows a function object to compile down to the exact same code as inlining the function body.

    It is what makes C++11 lambdas perfectly optimal for simple use cases:

    factorial = std::accumulate(begin, end, [] (int a,int b) { return a*b; });
    

    The lambda compiles down to a functor object similar to

    struct lambda_1
    {
         int operator()(int a, int b) const 
         { return a*b; }
    };
    

    The compiler sees that the constructor/destructor can be elided and the function body get’s inlined. The end result is optimal 1


    More (un)observable behaviour

    The standard contains a very entertaining example to the contrary, to spark your imagination.

    § 20.7.2.2.3

    [ Note: The use count updates caused by the temporary object construction and destruction are not
    observable side effects, so the implementation may meet the effects (and the implied guarantees) via
    different means, without creating a temporary. In particular, in the example:

    shared_ptr<int> p(new int);
    shared_ptr<void> q(p);
    p = p;
    q = p;
    

    both assignments may be no-ops. —end note ]

    IOW: Don’t underestimate the power of optimizing compilers. This in no way means that language guarantees are to be thrown out of the window!

    1 Though there could be faster algorithms to get a factorial, depending on the problem domain 🙂

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

Sidebar

Related Questions

Suppose you have the following code: int main(int argc, char** argv) { Foo f;
Suppose I have the following program: #include <stdio.h> int main() { printf(This is a
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have the following C code. unsigned int u = 1234; int i
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
Suppose I have the following directory layout in a Maven project: src/ |-- main
suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int
Suppose I have the following hierarchy: class A { public: A() private: int aa;
Suppose we have the following: void print() { int a; // declaration a =
Suppose I have the following code: typedef struct { char **p; } STRUCT; int

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.