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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:23:26+00:00 2026-06-04T16:23:26+00:00

I wrote the following code: class MyObjectHolder { public: std::vector<int> getMyObject() const { return

  • 0

I wrote the following code:

class MyObjectHolder {
public:
    std::vector<int> getMyObject() const {
        return myObject;
    }

private:
    std::vector<int> myObject;
};

At some point of my program I attempt to use the getMyObject method and use only const methods on the retrieved object:

const std::vector<int> myObject = myObjectHolder.getMyObject();
myObject.size();
int a = myObject.front();
  • Now, is it possible that the compiler will optimize this code so that no copies of the std::vector<int> are done?

    Is it somehow possible that the compiler determines that I’m only using the const methods on the retrieved object (and let’s assume there is no mutable nonsense happening behind it) and it would not make any copies of the objects and perform these const operations on the private member of the MyObjectHolder instead?

  • If yes, would it be possible if I didn’t explicitly declare the const std::vector<int> myObject as const?

  • If no, what are the reasons not to do this? In which cases this optimization would be to hard to implement / deduce that it’s possible and correct here / etc… ?

  • 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-04T16:23:27+00:00Added an answer on June 4, 2026 at 4:23 pm

    Now, is it possible that the compiler will optimize this code so that no copies of the std::vector<int> are done?

    No, the compiler doesn’t know what callers will do with that object unless you are making use of global optimization over all code that uses the object (the compiler can’t generally make assumptions about its use; moreover if object is exported from a dll it can’t make any assumption at all).

    If yes, would it be possible if I didn’t explicitly declare the const std::vector myObject as const?

    No, anyway the conversion from non-const to const could be implicit.

    If no, what are the reasons not to do this? In which cases this optimization would be to hard to implement / deduce that it’s possible and correct here / etc… ?

    It’s an optmiziation that should be done inside getMyObject() but the compiler can’t be sure that callers won’t cast away the const. Actually this is a very old debate about the use of const, usually I think it’s more clear to always think about const as something for programmers and not for compilers.

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

Sidebar

Related Questions

I wrote the following code: #include <iostream> class A { public: A(){ std::cout <<
i have wrote the following polymorphic code: public abstract class A { readonly int
I wrote the following code: public class Point2 { private double _radius , _alpha;
HI, I have the following code: class Libr { public: Libr(); std::string book; class
Given the following code : package core; public abstract class GeometricElement { private float
I wrote the following code: public class A { protected string Howdy = "Howdy!";
I wrote the following code. [XmlRoot(myxml)] public class myxml { [XmlElement(one)] public string one
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
I wrote the following code to count the node of an XML file: private
I wrote following code: class node: def __init__(self, title, series, parent): self.series = series

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.