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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:32:18+00:00 2026-06-01T11:32:18+00:00

Suppose I have this hypothetical, odd and unintuitive situation #include <iostream> struct A {

  • 0

Suppose I have this hypothetical, odd and unintuitive situation

    #include <iostream>

    struct A
    {
      A()
      {
        member = 1;
      }

      A(const A &)
      {
        member = 2;
      }

      int member;
    };

    int main()
    {
      A a = A();
      A b = a;
      std::cout << a.member << std::endl;
      std::cout << b.member << std::endl;
      return 0;
    }

I know that copy elision means that a will be initialized with just the default constructor and that b will be initialized with the copy constructor. I also know that (on gcc at least) you can tell the compiler not to do any copy elision.

My question is there some way of having the compiler not use copy elision just for this class?

I realize that the answer in any real situation will be to find some other way 99.9% of the time, and I don’t have one of those 0.01% cases (this is an actual hypothetical question, not a “hypothetical question”)

  • 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-01T11:32:19+00:00Added an answer on June 1, 2026 at 11:32 am

    Copy elision is allowed by the standard and it is the single optimization that is not required to follow the As-If rule[#1], So you should not rely on the behavior.

    You might use some compiler settings like in case of gcc, from the man page:

    -fno-elide-constructor

    The C++ standard allows an implementation to omit creating a temporary which is only used to initialize another object of the same type. Specifying this option disables that optimization, and
    forces G++ to call the copy constructor in all cases.

    However, using this makes your code non portable across different compilers.


    [#1] C++03 1.9 “Program execution:

    conforming implementations are required to emulate (only) the observable behavior of the abstract machine.

    The footnote further describes it in detail.

    This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no side effects affecting the observable behavior of the program are produced.

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

Sidebar

Related Questions

Suppose I have this struct: struct vector_data { double x, y; double& operator[](size_t index)
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose I have a hypothetical html layout such as this (while hypothetical, I'm implementing
Suppose I have this number list: List<int> = new List<int>(){3,5,8,11,12,13,14,21} Suppose that I want
Suppose I have this annotation class @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodXY { public int
Suppose I have this text to match: http://localhost:8080/start.jsp;jsessionid=9E4CDB636248C9610F57704E5E07F782?whatever=true&somethingelse=true Using this regular expression: ^(.*?start\.jsp)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$ The
suppose I have this function: function f($string){ $string = preg_replace(`\[.*\]`U,,$string); $string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$string); $string
Suppose I have this struct (which incidentally contain bit-fields, but you shouldn't care): struct
Suppose I have this code: static void Main(string[] args) { var thread = new
Suppose we have this table.. CREATE TABLE `appointments` ( `idappointments` int(11) NOT NULL AUTO_INCREMENT,

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.