C++ Logo

std-proposals

Advanced search

Re: Fixing some initialization gotchas

From: Matthew Woehlke <mwoehlke.floss_at_[hidden]>
Date: Fri, 23 Aug 2019 18:26:15 -0400
On 23/08/2019 17.00, Hyman Rosen wrote:
> You say when you look at `auto x = {1}`, the only logical explanation in
> your mind is that the RHS is an initializer list but you have no intuition
> for `auto x{1}`. But in the context of teaching C++, why would that be?

In the first case, I am assigning "something" to an automatically typed
variable `x`, where "something" is `{1}`. Well, just on its own like
that, the RHS "looks like" an initializer list, so today's behavior is
at least plausible.

In the second case, I struggle to come up with any reasonable
explanation for why it's *not* ill-formed. To my mind, `auto x{1}` looks
like nonsense. I *might* actually be able to convince myself it should
be an initializer list, same as the first case. I'm just not seeing why
I would ever expect it to be `int`.

> What is it about the presence or absence of the `=` that will speak to
> the intuition of the student? Are students supposed to know about
> initializer lists at all? Aren't they supposed to be behind-the scenes
> magic that let you write intuitive initializers without thinking about how
> they work?

Perhaps. My knee-jerk reaction was that I don't want to have to write
out `std::initializer_list`. OTOH, I'm not sure when I'd actually want
one of those, anyway. (I did actually try to do something like this just
recently, which probably has something to do with my reaction, but it
didn't wind up working out... and better constexpr stuff is probably a
better solution to what I was trying to do, anyway.)

-- 
Matthew

Received on 2019-08-23 17:28:19