Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ references - are they just syntactic sugar?

Tags:

c++

Is a C++ reference just syntactic sugar, or does it offer any speed ups in certain cases?

For example, a call-by-pointer involves a copy anyway, and that seems to be true about a call-by-reference as well. The underlying mechanism appears to be the same.

Edit: After about six answers and many comments. I am still of the opinion references are just syntatic sugar. If people could answer in a straight yes or no, and if someone could do an accepted answer?

like image 885
samofoz Avatar asked Jul 07 '15 08:07

samofoz


People also ask

Are references just pointers?

A reference is the object. It is not a pointer to the object, nor a copy of the object.

What is meant by syntactic sugar?

In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

Are there references in C?

No, it doesn't.

What does a reference provide in C?

A reference automatically gets dereferenced to the location it point to. It is typically used in association with function argument list and return values. It has many other uses apart from this; a reference also can be used as a freestanding reference.


2 Answers

Assume reference as a pointer that:

  1. Can't be NULL
  2. Once initialized, can't be re-pointed to other object
  3. Any attempt to use it will implicitly dereference it:

    int a = 5;
    int &ra = a;
    int *pa = &a;
    
    ra = 6;
    
    (*pa) = 6;
    

here as it looks in disassembly:

    int a = 5;
00ED534E  mov         dword ptr [a],5  
    int &ra = a;
00ED5355  lea         eax,[a]  
00ED5358  mov         dword ptr [ra],eax  
    int *pa = &a;
00ED535B  lea         eax,[a]  
00ED535E  mov         dword ptr [pa],eax  

    ra = 6;
00ED5361  mov         eax,dword ptr [ra]  
00ED5364  mov         dword ptr [eax],6  

    (*pa) = 6;
00ED536A  mov         eax,dword ptr [pa]  
00ED536D  mov         dword ptr [eax],6  

the assigning to the reference is the same thing from the compiler perspective as the assigning to a dereferenced pointer. There are no difference between them as you can see (we are not talking about compiler optimization right now) However as mentioned above, references can't be null and have stronger guarantees of what they contains.

As for me, I prefer using references as long as I don't need nullptr as a valid value, values that should be repointed or values of different types to be passed into (e.g. pointer to interface type).

like image 56
VinSmile Avatar answered Nov 07 '22 06:11

VinSmile


References have stronger guarantees than pointers, so the compiler can optimize more aggressively. I've recently seen GCC inline multiple nested calls through function references perfectly, but not a single one through function pointers (because it couldn't prove that the pointer was always pointing at the same function).

If the reference ends up stored somewhere, it typically takes the same space as a pointer. That is not to say, again, that it will be used like a pointer : the compiler may well cut through it if it knows which object the reference was bound to.

like image 38
Quentin Avatar answered Nov 07 '22 07:11

Quentin