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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:05:59+00:00 2026-06-16T01:05:59+00:00

Implementation 1: foo(const Bar x); Implementation 2: foo(const Bar & x); If the object

  • 0

Implementation 1:

foo(const Bar x);

Implementation 2:

foo(const Bar & x);

If the object will not be changed within the function, why would you ever copy it(implementation 1).

Will this be automatically optimized by the compiler?

Summary: Even though the object is declared as const in the function declaration, it is still possible that the object be edited via some other alias &.

If you are the person writing the library and know that your functions don’t do that or that the object is big enough to justify the dereferencing cost on every operation, than
foo(const Bar & x); is the way to go.

Part 2:

Will this be automatically optimized by the compiler?

Since we established that they are not always equivalent, and the conditions for equivalence is non-trivial, it would generally be very hard for the compiler to ensure them, so almost certainly no

  • 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-16T01:06:00+00:00Added an answer on June 16, 2026 at 1:06 am

    you ask,

    “If the object will not be changed within the function, why would you ever copy it(implementation 1).”

    well there are some bizarre situations where an object passed by reference might be changed by other code, e.g.

    namespace g { int x = 666; }
    
    void bar( int ) { g::x = 0; }
    
    int foo( int const& a ) { assert( a != 0 );  bar( a );  return 1000/a; }  // Oops
    
    int main() { foo( g::x ); }
    

    this has never happened to me though, since the mid 1990s.

    so, this aliasing is a theoretical problem for the single argument of that type.

    with two arguments of the same type it gets more of a real possibility. for example, an assignment operator might get passed the object that it’s called on. when the argument is passed by value (as in the minimal form of the swap idiom) it’s no problem, but if not then self-assignment generally needs to be avoided.

    you further ask,

    “Will this be automatically optimized by the compiler?”

    no, not in general, for the above mentioned reason

    the compiler can generally not guarantee that there will be no aliasing for a reference argument (one exception, though, is where the machine code of a call is inlined)

    however, on the third hand, the language could conceivably have supported the compiler in this, e.g. by providing the programmer with a way to explicitly accept any such optimization, like, a way to say ”this code is safe to optimize by replacing pass by value with pass by reference, go ahead as you please, compiler”

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

Sidebar

Related Questions

Are there some drawbacks of such implementation of copy-constructor? Foo::Foo(const Foo& i_foo) { *this
Which is best practice (in this case): bool Foo::operator==(const Foo& other) { return bar
I have a trait and an implementation looking like: trait Foo[A] { def bar[B
How would you serialize/deserialize this class using boost::serialization? #include <vector> struct Foo { struct
Sorry if it a trivial question: Implementation 1: class Foo { protected: int bar;
@interface Foo : NSObject @property (nonatomic, retain) Bar * bar; @end @implementation Foo @synthesize
I have a class: template<typename T> class Foo { public: ... Foo& operator=(const Foo&)
I can't seem to make boost::ptr_unordered_map<uint32_t, const Foo> work - the underlying implementation looks
Can I not do this in objective c? @interface Foo : NSObject { int
I have a piece of code similar to this: //Foo.h OBJC_EXPORT MyObject *const myObj;

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.