Why isn't type& treated as a single token?

This is a why does the language work this way question, but I've just spent an extremely stressful day trying to find a problem and it turned out that I had declared some references in a class like this:
1
2
3
class a{
int& a,b,c,d;
}

What I didn't realise is that this means that only a is a reference, and the rest were being initialised as copies. Seeing as C++ treats int& and int as different types, why isn't the type of int& applied to the whole list? After all, this means that you can declare things of different types in the same list.
Last edited on
> Seeing as C++ treats int& and int as different types, why isn't the type of int& applied to the whole list?

For reasons of compatibility with the declaration syntax of C.

A "typical C programmer'' writes "int *p;'' and explains it "*p is what is the int'' emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

A "typical C++ programmer'' writes "int* p;'' and explains it "p is a pointer to an int'' emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.
...
See The Design and Evolution of C++ for a longer discussion of the C declaration syntax.
http://www.stroustrup.com/bs_faq2.html#whitespace


Creating a type alias would help if we must have more than one name per declaration.
1
2
3
4
5
6
struct A {
 
    using reference = int& ;
    reference a, b, c, d ;
    // ...
};
Topic archived. No new replies allowed.