Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #85809

Re: initializer list type deduction rules

From Bo Persson <bo@bo-persson.se>
Newsgroups comp.lang.c++
Subject Re: initializer list type deduction rules
Date 2022-08-09 10:43 +0200
Message-ID <jlel24FdqhsU1@mid.individual.net> (permalink)
References <tct3rm$1l9a$1@gioia.aioe.org>

Show all headers | View raw


On 2022-08-09 at 09:54, Juha Nieminen wrote:
> The exact type deduction rules for initializer lists are not completely
> clear to me. For example, in an expression like this the type is deduced
> without problems:
> 
>    std::pair<unsigned, unsigned> values[] =
>        { { 1, 2 }, { 2, 3 }, { 3, 4 } };
> 
> However, here it's not:
> 
>    for(const std::pair<unsigned, unsigned>& value:
>            { { 1, 2 }, { 2, 3 }, { 3, 4 } })
> 
> Sometimes it's useful to loop through a list of values using a range-based
> for loop one-liner:
> 
>    for(int i: { 10, 2, 15, 28, 3 })
> 
> However, because of the limitations of deducing the type of the
> initializer list in such range-based for expressions, that's not always
> that simple, as in the second example above.
> 
> Even in such a simple case as this:
> 
>    unsigned values[] = { 1, 2, 3, 5 10 };
> 
> vs. this:
> 
>    for(unsigned value: { 1, 2, 3, 5, 10 })
> 
> if you turn on enough compiler warnings eg. gcc will warn about the
> second one (converting 'int' to 'unsigned' may change the sign of the
> result) but not the first one. I believe that in the first case the
> compiler deduces the type of the initializer list to be 'unsigned'
> (even though the literals themselves are of type 'int'), while in
> the second case it does not.
> 
> I kind of grasp the difference between these two cases, but it just
> makes range-based for loops less convenient when it's like this.

I beleive the rules are pretty clear, but that the use cases are not 
equally common.

For example, in

unsigned values[] = { 1, 2, 3, 5 10 };

you would get tons of false warnings in many programs. So the compiler 
has gotten code to verify that there are in fact no negative values, and 
to suppress the warning.

The same *could* happen for the ranged-for case, but apparently it does 
not. My guess is that there a significantly fewer cases, and the 
"warning suppression" has not gotten a high enough priority.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

initializer list type deduction rules Juha Nieminen <nospam@thanks.invalid> - 2022-08-09 07:54 +0000
  Re: initializer list type deduction rules Bo Persson <bo@bo-persson.se> - 2022-08-09 10:43 +0200
  Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-09 09:00 -0700
    Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-09 16:06 +0000
      Re: initializer list type deduction rules Juha Nieminen <nospam@thanks.invalid> - 2022-08-10 02:32 +0000
        Re: initializer list type deduction rules Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-10 09:11 +0300
          Re: initializer list type deduction rules Juha Nieminen <nospam@thanks.invalid> - 2022-08-10 06:57 +0000
        Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-10 07:31 +0000
      Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-10 00:40 -0700
        Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-10 07:55 +0000
          Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-10 01:13 -0700
            Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-10 08:21 +0000
              Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-10 11:13 -0700
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-12 08:26 +0000
                Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-12 03:38 -0700
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-12 10:48 +0000
                Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-12 04:54 -0700
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-12 14:34 +0000
                Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-12 08:34 -0700
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-12 15:57 +0000
                Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-12 09:16 -0700
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-13 09:35 +0000
                Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-13 09:44 -0700
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-14 11:12 +0000
                Re: initializer list type deduction rules scott@slp53.sl.home (Scott Lurndal) - 2022-08-12 14:58 +0000
                Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-12 08:59 -0700
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-12 16:11 +0000
                Re: initializer list type deduction rules scott@slp53.sl.home (Scott Lurndal) - 2022-08-12 16:44 +0000
                Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-12 12:08 -0700
                Re: initializer list type deduction rules scott@slp53.sl.home (Scott Lurndal) - 2022-08-12 19:58 +0000
                Re: initializer list type deduction rules Öö Tiib <ootiib@hot.ee> - 2022-08-12 16:42 -0700
                Re: initializer list type deduction rules David Brown <david.brown@hesbynett.no> - 2022-08-13 18:14 +0200
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-14 11:06 +0000
                Re: initializer list type deduction rules David Brown <david.brown@hesbynett.no> - 2022-08-15 11:34 +0200
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-15 19:03 +0000
                Re: initializer list type deduction rules David Brown <david.brown@hesbynett.no> - 2022-08-15 21:23 +0200
                Re: initializer list type deduction rules red floyd <no.spam.here@its.invalid> - 2022-08-15 13:51 -0700
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-16 19:16 +0000
                Re: initializer list type deduction rules Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-21 22:23 +0300
                Re: initializer list type deduction rules David Brown <david.brown@hesbynett.no> - 2022-08-22 08:08 +0200
                Re: initializer list type deduction rules Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-22 11:37 +0300
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-22 10:23 +0000
          Re: initializer list type deduction rules Juha Nieminen <nospam@thanks.invalid> - 2022-08-11 06:24 +0000
            Re: initializer list type deduction rules Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-11 16:37 +0100
              Re: initializer list type deduction rules Juha Nieminen <nospam@thanks.invalid> - 2022-08-11 20:12 +0000
                Re: initializer list type deduction rules Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-12 00:09 +0100
                Re: initializer list type deduction rules Juha Nieminen <nospam@thanks.invalid> - 2022-08-12 11:51 +0000
            Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-12 08:29 +0000
              Re: initializer list type deduction rules Juha Nieminen <nospam@thanks.invalid> - 2022-08-12 12:08 +0000
                Re: initializer list type deduction rules Muttley@dastardlyhq.com - 2022-08-12 14:37 +0000
                Re: initializer list type deduction rules scott@slp53.sl.home (Scott Lurndal) - 2022-08-12 15:01 +0000
                Re: initializer list type deduction rules Juha Nieminen <nospam@thanks.invalid> - 2022-08-15 06:04 +0000
  Re: initializer list type deduction rules Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-09 16:08 -0700
    Re: initializer list type deduction rules Juha Nieminen <nospam@thanks.invalid> - 2022-08-10 02:34 +0000

csiph-web