-1

Can someone explain what the difference is, if any, between these two lines:

int& i;
int &i;

I know these are both references and both seem to work fine. Is there a reason to use one over the other? Is there any rule saying what is the right way?

Thanks in advance.

  • For the obvious: one has a space before the '&' and the other doesn't. In parsing, there doesn't need to be a space before or after the '&'. So you forgot a 3rd equivalent example: `int&i;`. – Thomas Matthews Jan 02 '19 at 21:03

1 Answers1

3

There is absolutely no difference in meaning between these two, it is a purely stylistic matter. Just pick one and try to be consistent within a project.

I believe the examples in the language standard put the & symbol on the left - that's as good a reason as any to prefer one way over the other, I suppose.


That said, as you've written it, neither line is valid code, because you can't have an uninitialised reference. You would need something like:

int a = 10;
int& b = a;
BoBTFish
  • 19,167
  • 3
  • 49
  • 76