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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:21:44+00:00 2026-05-27T14:21:44+00:00

Recently, I have been developing a practice of making many things in my code

  • 0

Recently, I have been developing a practice of making many things in my code as const:

(1) Arguments to function, which I know never going to be changed. e.g.:

void foo (const int i, const string s)
          ^^^^^        ^^^^^ 

(2) Return types as const. e.g.:

struct A {
...
  const int foo () { return ...; }
  ^^^^^
  operator const bool () const { return ...; }
           ^^^^^
};

(3) Trivial computation of integer or strings. e.g.:

const uint size = vec.size();
^^^^^
const string s2 = s1 + "hello ";
^^^^^

… and few more places. Typically in other real world codes, I don’t see such small scale variables marked as const. But I thought, making them const will never harm. Is it a good programming practice ?

  • 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-27T14:21:45+00:00Added an answer on May 27, 2026 at 2:21 pm

    (1) and (3) are closely related. A by-value parameter is just a local variable with that name, as is the result of your computation.

    Usually it makes little difference in short functions whether you mark local variables const or not, since you can see their entire scope right in front of you. You can see whether or not the value changes, you don’t need or want the compiler to enforce it.

    Occasionally it does help, however, since it protects you from accidentally passing them to a function that takes its parameter by non-const reference, without realising that you’re modifying your variable. So if you pass the variable as a function argument during its life, then marking it const can give you more confidence that you know what value it has afterwards.

    Very occasionally, marking a variable const can help the optimizer, since you’re telling it that the object is never modified, and sometimes that’s true but the compiler can’t otherwise prove it. But it’s probably not worth doing it for that reason, because in most cases it makes no difference.

    (2) is another matter. For built-in types it makes no difference, as others have explained. For class types, do not return by const value. It might seem like a good idea, in that it prevents the user writing something pointless like func_returning_a_string() += " extra text";. But it also prevents something which is pointful — C++11 move semantics. If foo returns a const string, and I write std::string s = "foo"; if (condition) s = foo();, then I get copy assignment at s = foo();. If foo returns a non-const string then I get move assignment.

    Similarly in C++03, which doesn’t have move semantics, it prevents the trick known as “swaptimization” – with a non-const return value I can write foo().swap(s); instead of s = foo();.

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

Sidebar

Related Questions

I have recently been developing a website which relies a large part on the
I recently launched a website I have been developing over the past year -
I've recently been developing on the django platform and have stumbled upon Django Forms
Recently, I was going through an open-source project and although I have been developing
I have been developing web applications for some time but have very recently been
I am currently developing on an advertising system, which have been running just fine
I have been developing on Android with Eclipse for a while, and I recently
i have been recently studying php and mysql and also is currently developing a
Recently I have been developing an app that needs AES encryption. I am using
I have been developing a magazine engine which behaves like a web page. Currently

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.