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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:40:22+00:00 2026-05-14T00:40:22+00:00

I recently had an exchange with another C++ developer about the following use of

  • 0

I recently had an exchange with another C++ developer about the following use of const:

void Foo(const int bar);

He felt that using const in this way was good practice.

I argued that it does nothing for the caller of the function (since a copy of the argument was going to be passed, there is no additional guarantee of safety with regard to overwrite). In addition, doing this prevents the implementer of Foo from modifying their private copy of the argument. So, it both mandates and advertises an implementation detail.

Not the end of the world, but certainly not something to be recommended as good practice.

I’m curious as to what others think on this issue.

Edit:

OK, I didn’t realize that const-ness of the arguments didn’t factor into the signature of the function. So, it is possible to mark the arguments as const in the implementation (.cpp), and not in the header (.h) – and the compiler is fine with that. That being the case, I guess the policy should be the same for making local variables const.

One could make the argument that having different looking signatures in the header and source file would confuse others (as it would have confused me). While I try to follow the Principle of Least Astonishment with whatever I write, I guess it’s reasonable to expect developers to recognize this as legal and useful.

  • 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-14T00:40:22+00:00Added an answer on May 14, 2026 at 12:40 am

    Remember the if(NULL == p) pattern ?

    There are a lot of people who will tell a "you must write code like this":

    if(NULL == myPointer) { /* etc. */ }
    

    instead of

    if(myPointer == NULL) { /* etc. */ }
    

    The rationale is that the first version will protect the coder from code typos like replacing "==" with "=" (because it is forbidden to assign a value to a constant value).

    The following can then be considered an extension of this limited if(NULL == p) pattern:

    Why const-ing params can be useful for the coder

    No matter the type, "const" is a qualifier that I add to say to the compiler that "I don’t expect the value to change, so send me a compiler error message should I lie".

    For example, this kind of code will show when the compiler can help me:

    void bar_const(const int & param) ;
    void bar_non_const(int & param) ;
    
    void foo(const int param)
    {
       const int value = getValue() ;
    
       if(param == 25) { /* Etc. */ } // Ok
       if(value == 25) { /* Etc. */ } // Ok
    
       if(param = 25) { /* Etc. */ } // COMPILE ERROR
       if(value = 25) { /* Etc. */ } // COMPILE ERROR
    
       bar_const(param) ;  // Ok
       bar_const(value) ;  // Ok
    
       bar_non_const(param) ;  // COMPILE ERROR
       bar_non_const(value) ;  // COMPILE ERROR
    
       // Here, I expect to continue to use "param" and "value" with
       // their original values, so having some random code or error
       // change it would be a runtime error...
    }
    

    In those cases, which can happen either by code typo or some mistake in function call, will be caught by the compiler, which is a good thing.

    Why it is not important for the user

    It happens that:

    void foo(const int param) ;
    

    and:

    void foo(int param) ;
    

    have the same signature.

    This is a good thing, because, if the function implementer decides a parameter is considered const inside the function, the user should not, and does not need to know it.

    This explains why my functions declarations to the users omit the const:

    void bar(int param, const char * p) ;
    

    to keep the declaration as clear as possible, while my function definition adds it as much as possible:

    void bar(const int param, const char * const p)
    {
       // etc.
    }
    

    to make my code as robust as possible.

    Why in the real world, it could break

    I was bitten by my pattern, though.

    On some broken compiler that will remain anonymous (whose name starts with "Sol" and ends with "aris CC"), the two signatures above can be considered as different (depending on context), and thus, the runtime link will perhaps fail.

    As the project was compiled on a Unix platforms too (Linux and Solaris), on those platforms, undefined symbols were left to be resolved at execution, which provoked a runtime error in the middle of the execution of the process.

    So, because I had to support the said compiler, I ended polluting even my headers with consted prototypes.

    But I still nevertheless consider this pattern of adding const in the function definition a good one.

    Note: Sun Microsystems even had the balls to hide their broken mangling with an "it is evil pattern anyway so you should not use it" declaration. see http://docs.oracle.com/cd/E19059-01/stud.9/817-6698/Ch1.Intro.html#71468

    One last note

    It must be noted that Bjarne Stroustrup seems to be have been opposed to considering void foo(int) the same prototype as void foo(const int):

    Not every feature accepted is in my opinion an improvement, though. For example, […] the rule that void f(T) and void f(const T) denote the same function (proposed by Tom
    Plum for C compatibility reasons) [have] the dubious distinction of having been voted into C++ “over my dead body”.

    Source: Bjarne Stroustrup
    Evolving a language in and for the real world: C++ 1991-2006, 5. Language Features: 1991-1998, p21.
    http://www.stroustrup.com/hopl-almost-final.pdf

    This is amusing to consider Herb Sutter offers the opposite viewpoint:

    Guideline: Avoid const pass-by-value parameters in function declarations. Still make the parameter const in the same function’s definition if it won’t be modified.

    Source: Herb Sutter
    Exceptional C++, Item 43: Const-Correctness, p177-178.

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

Sidebar

Related Questions

I recently had a discussion on another forum with another developer and the topic
I recently had to work on a project where the previous developer modified the
We recently had a new requirement to use the phonon component of Qt, which
I recently had a need to interpret a DEC 32-bit floating point representation. It
We recently had a code review . One of my classes was used so
I recently had a problem during the deployment of a windows service. Four computers
I recently had a problem in my app where some of the subviews I
I recently had to take a quick look at Adobe InDesign server. In this
I recently had some problems with my VS 2008, and was recommended to reinstall.
I recently had cause to work with some Visual Studio C++ projects with the

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.