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

Re: auto

38 views
Skip to first unread message
Message has been deleted

Victor Bazarov

unread,
May 3, 2015, 8:25:39 PM5/3/15
to
On 5/3/2015 12:39 PM, Stefan Ram wrote:
> I posted this program into another newsgroup, where the
> topic was how short a program to calculate a product of
> sums of values can be in various programming languages:
>
> #include <iostream>
> #include <ostream>
> #include <initializer_list>
>
> int main()
> { auto const X ={ 1, 2 };
> auto const Y ={ 3, 4 };
> double r = 1.0;
> for( int const x : X )for( int const y : Y )r *= x + y;
> ::std::cout << r << '\n'; }
>
> . Then, I wondered how short I can make it when I do not
> follow my usual stylistic guidelines. The following still
> works under the C++ implementation used:
>
> #include <iostream>
> int main()
> {auto X={1,2},Y={3,4},r=1;for(int x:X)for(int y:Y)r*=x+y;std::cout<<r;}
>
> But why is
>
> auto X={1,2},Y={3,4},r=1;
>
> actually possible? The type deduced for »{1,2}« should be
> «initializer_list<int>«, while the type deduced for »r«
> should be »int«, so it's not the same type!

It is allowed, I think. See [dcl.spec.auto]. The types need to match,
but std::initializer_list<T> is a special case, I think. In other
words, in this case 'auto' is determined as 'int' but X is made
'std::initializer_list<int>' (from the brace-enclosed initializer).

> (If you would write »r=1.«, then the implementation /would/
> complain that the types do not match.)

V
--
I do not respond to top-posted replies, please don't ask
0 new messages