Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When auto can and can not deduce const

40 views
Skip to first unread message

Paul

unread,
Jun 13, 2015, 8:14:01 AM6/13/15
to
Below is quoted from a C++ book. I don't understand why the cases auto& g = ci; and auto &h = 42; are different. auto& h = 42; is an error because we can't bind a plain reference to a literal. So why is auto& g = ci; allowed? If auto& h = 42 binds a plain reference to a literal then why doesn't auto& g = ci; bind a plain reference to a const int?

Many thanks for your help,
Paul



BEGIN QUOTE
int i = 0, &r = i;
auto a = r; // a is an int (r is an alias for i, which has type int)

Second, auto ordinarily ignores top-level consts. As usual in initializations, low-level consts, such as when an initializer is a pointer to const, are kept

const int ci = i, &cr = ci; auto b = ci; // b is an int (top-level const in ci is dropped)

auto c = cr; // c is an int (cr is an alias for ci whose const is top-level) auto d = &i; // d is an int*(& of an int object is int*)
auto e = &ci; // e is const int*(& of a const object is low-level const) If we want the deduced type to have a top-level const, we must say so explicitly
const auto f = ci; // deduced type of ci is int; f has type const int We can also specify that we want a reference to the auto-deduced type. Normal initialization rules still apply
auto &g = ci; // g is a const int& that is bound to ci
auto &h = 42; // error: we can't bind a plain reference to a literal
const auto &j = 42; // ok: we can bind a const reference to a literal
END QUOTE
Message has been deleted
Message has been deleted

Paul

unread,
Jun 13, 2015, 10:31:47 AM6/13/15
to
On Saturday, June 13, 2015 at 1:56:22 PM UTC+1, Stefan Ram wrote:
> Paul <peps...@gmail.com> quotes:
> >const auto &j = 42; // ok: we can bind a const reference to a literal
>
> The reference is /not/ bound to the literal, but to the
> /value/ of the literal!
>
> Justification:
>
> A literal is a part of the source-code model, a value is a
> part of the run-time model. A reference is a part of the
> run-time model; it can only bind to other run-time entities.
>
> Example:
>
> const auto &i = 042;
> const auto &j = 0042;
>
> These are two different literals, but they have the same value.
> Where the references bound to the literals, it should be possible
> two see the difference between the literals using the references.
> But it is not. The reference are bound to the /values/ of the literals,
> and the values are the same, so the value of the references also
> is the same.

Thanks. The error is in the text I'm quoting from not mine (not that anyone said otherwise).

Paul

Paul

unread,
Jun 13, 2015, 10:37:24 AM6/13/15
to
On Saturday, June 13, 2015 at 1:44:42 PM UTC+1, Stefan Ram wrote:
> Paul <peps...@gmail.com> writes:
> >const int ci = i
> ...
> >why is auto& g = ci; allowed
>
> »A const object is an object« 3.9.3p1.1

Sorry, I still don't get it. The examples below are totally unclear to me. Why is auto &h = 42; wrong? If the answer is because an auto should refer to an object, not a value then why is const auto& j = 42; ok?

Is the rule that auto can't refer to a value but const auto can?

Paul

auto &g = ci; // g is a const int& that is bound to ci
auto &h = 42; // error: we can't bind a plain reference to a value

Richard Damon

unread,
Jun 13, 2015, 11:14:13 AM6/13/15
to
The issue isn't auto, but what a reference can bind to.

int &h = 42; // error
int const&j = 42; // ok

const references can bind to a temporary, regular references can't.

42 has type "int", if it had type "const int" then auto would work.
(Maybe that is a 'defect' in the standard??)

Paul

unread,
Jun 13, 2015, 11:30:12 AM6/13/15
to
Thanks, I understand now.

Paul

skyne...@gmail.com

unread,
Jun 15, 2015, 6:55:18 PM6/15/15
to
1-800 AUTO YES is the top performing, highest paying auto-loan lead generating program. Our performance pricing gives publishers the opportunity to earn enormous cash - as much as $12.00 per lead just by providing users with a FREE online auto loan application form. It takes seconds to apply resulting in incredible conversions for you .
http://www.1800freshstart.com/?src=42051&lnk=74248
0 new messages