Note that there are some explanatory texts on larger screens.

plurals
  1. POTop-level const doesn't influence a function signature
    primarykey
    data
    text
    <p>From the C++ Primer 5th Edition, it says:</p> <pre><code>int f(int){ /* can write to parameter */} int f(const int){ /* cannot write to parameter */} </code></pre> <p>The two functions are <strong>indistinguishable</strong>. But as you know, the two functions really differ in how they can update their parameters.</p> <p>Can someone explains to me?</p> <hr> <p><strong>EDIT</strong><br> I think I didn't interpret my question well. What I really care is why C++ doesn't allow these two functions simultaneously as different function since they are really different as to "whether parameter can be written or not". Intuitively, it should be!</p> <hr> <p><strong>EDIT</strong><br> The nature of <em>pass by value</em> is actually <em>pass by coping argument values to parameter values</em>. Even for <em>references</em> and <em>pointers</em> where thee copied values are <em>addresses</em>. From the caller's viewpoint, whether <em>const</em> or <em>non-const</em> is passed to the function does not influence values (and of course types of) copied to parameters.<br> The distinction between <strong>top-level const</strong> and <strong>low-level const</strong> matters when copying objects. More specifically, <strong>top-level const</strong>(not the case of <strong>low-level const</strong>) is ignored when copying objects since copying won't influence the copied object. It is immaterial whether the object copied to or copied from is <em>const</em> or not.<br> So for the caller, differentiating them is not necessary. Likely, from the function viewpoint, the <em>top-level const</em> parameters doesn't influence the interface and/or the functionality of function. The two function actually accomplish the same thing. Why bother implementing two copies?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload