3

Well I didnt realize that const could be as confusing as pointers. Could someone please explain in steps what exactly does the following code do in terms of const?

const int*const Method3(const int*const&)const;

It is so confusing even for the non novice programmer.

gfdsal
  • 651
  • 1
  • 8
  • 25
  • To answer your title question, it may help the compiler with optimizations in certain cases. The best benefit you'll get off writing 'const' is when someone else comes and reads your code. As for your OP, I'll leave it to the written answers below. – AlexG May 29 '17 at 16:50
  • `const int*const` is aweful, in my opinion. If you're going to have several const, then `int const *const` seems to be a much clearer syntax. – n.caillou May 29 '17 at 16:55
  • This question is asked often. Take a look here for more answers: https://stackoverflow.com/questions/3888470/c-const-member-function-that-returns-a-const-pointer-but-what-type-of-const – Blair Fonville May 29 '17 at 16:55
  • Returning a constant pointer is... well... pointless. You can just write `const int* p = Method3(...)` to turn that into a non-const pointer. – zett42 May 29 '17 at 16:56
  • 1
    Possible duplicate of [c++ const member function that returns a const pointer.. But what type of const is the returned pointer?](https://stackoverflow.com/questions/3888470/c-const-member-function-that-returns-a-const-pointer-but-what-type-of-const) – Blair Fonville May 29 '17 at 16:56

3 Answers3

5

It's probably confusing because it's mixing two styles of const together.

const int*const Method3(const int*const&)const;

I will reorder them because the best way to understand these is to read them backwards, in my opinion.

Let's start with the return type:

const int*const -> int const* const

By reading it backwards we get: const pointer to const int.

Similarly, for the function parameter:

const int* const& -> int const* const&

By reading it backwards we get: reference to const pointer to const int.

The function is also marked as const, which means it is a member function that can be called when a reference to that class is constant, for example.

For possible const optimizations and further understanding, see these answers:

user2296177
  • 2,807
  • 1
  • 15
  • 26
  • 1
    See also (shameless plug) https://stackoverflow.com/questions/38925121/are-there-any-downsides-to-marking-all-variables-you-dont-modify-const/38925200#38925200 – Jesper Juhl May 29 '17 at 16:54
  • @JesperJuhl Added. – user2296177 May 29 '17 at 16:55
  • @user2296177 I do have a followup question why do we have a reference and a pointer in function argument, I though we either pass by reference, or by pointer or value, is pass by reference pointer a thing? – gfdsal May 29 '17 at 17:37
  • 2
    @GENIVI-LEARNER A reference to a pointer is indeed a thing. Read up on references, they're mostly syntactic sugar for pointers. You can declare `int** x` which is a pointer to a pointer to `int` or you can declare `int*& x` which is a reference to a pointer to `int`. The same rules that apply to `int*` vs `int&` apply here, the only difference is that the value is now `int*` instead of `int`. Consider an alias to make it clearer: `using pint = int*`, the two examples become `pint* x` and `pint& x`. – user2296177 May 29 '17 at 17:41
  • 2
    @GENIVI-LEARNER Taking a reference to a pointer allows the function to modify the pointer itself (instead of taking the pointer by value, where reassigning the passed pointer to another target doesn't modify the original pointer). Of course, taking it as a reference to a `const` pointer makes this moot, because `const` pointers can't be retargeted. – Justin Time - Reinstate Monica May 29 '17 at 18:06
  • @Justin Time It makes so much sense now! Thanks a lot! – gfdsal May 30 '17 at 18:20
  • 1
    @GENIVI-LEARNER You're welcome. Basically, everything is technically passed by value; to pass something by reference, a reference to it (which can be either a pointer or a reference) is instead passed by value, so that the value it refers to can be modified (if not `const`). That's where taking a reference to a pointer comes in; it allows both the pointer itself, and the entity it points to, to be accessed and modified. – Justin Time - Reinstate Monica May 30 '17 at 18:42
3

const tells the compiler that something is read only or refers to read only data (or both). That's its primary job and it helps the compiler warn you (through compilation errors) when you accidentally modify something you did not intend to / shouldn't.

Also, marking member functions const (when they don't modify the object) lets them be used in more contexts.

The optimizer may also use the presence of const to help it make optimizations to your code iff it can prove that you really don't modify that const thing. But due to things like "aliasing", volatile, mutable and const_cast (including C-style casts) existing in the language, it is rather constrained in its ability to do so in most cases.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • 2
    Todays optimizers are smart enough to prove if a variable isn't actually modified, whether or not it is marked `const`. I recently compared the disassembly of some code with and without const and I saw exactly the same optimization: the variable was turned into a literal instead of reading it from memory. – zett42 May 29 '17 at 17:03
  • 1
    @zett42 sure, they are pretty clever, but they *may* be helped further by `const` in some cases. See for example https://docs.google.com/document/d/112O-Q_XrbrU1I4P-oiLCN9u86Qg_BYBdcDsmh7Pn9Nw – Jesper Juhl May 29 '17 at 17:09
2

break it down:

const int*const Method3(const int*const&)const;
  ^         ^             ^          ^      ^  
  |         |             |          |      |
  |         |             |          |      |-> you can use it in objects declared as constants
  |         |             |          | 
  |         |             |          | ->  you get a const pointer to const int(the paramater can no be changed in any form)
  |         |              
  |         | ->  you get a const pointer to const int(the returned value can no be changed in any form)            
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Your ASCII art is missing the description for the line pointing to the first and third `const`. – Jesper Juhl May 29 '17 at 17:03
  • @JesperJuhl The first and second `const`s use the same descriptions, as do the third and fourth. The art isn't clear about that, though, the arrows should start from 1 & 3 and go through 2 & 4, instead of starting from 2 & 4. – Justin Time - Reinstate Monica May 29 '17 at 18:19