A
A
Artem2022-01-09 12:41:02
C++ / C#
Artem, 2022-01-09 12:41:02

What does a reference without a type mean?

long x = 10;

const &y = x;
cout << typeid(y).name() << '\n'; // тип int, а не long

x += 10;
cout << y; // 10, а не 20


Maybe the link took x as an r-value? But then why is int automatically after const? If you write const x = 10, then there will be an error

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Shatunov, 2022-01-09
@Trame2771

const &y = x;
It's not a link without a type, but syntactically invalid code that won't get translated outside of GCC.
Because If you're using GCC, you should be aware that it doesn't follow the standard in some cases.
Particularly in this case.
C++ does not have a default type, unlike C, where the default type is int. If in C the code const y = xis syntactically correct and implies const int y = x, then in C++ the same code is already syntactically incorrect and will not be translated .
GCC in your code is moving away from the C++ standard in favor of C-like behavior.

V
Vitaly, 2022-01-09
@vt4a2h

Always compile with -Wall -Wextra -pedantic flags and you will be happy :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question