Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Why this LOC is valid <int something = {42};> (http://www.velocityreviews.com/forums/t805449-why-this-loc-is-valid-int-something-42-a.html)

MJ_India 11-02-2011 01:30 PM

Why this LOC is valid <int something = {42};>
 
C++ file with following code compiles without any complaint on GCC and
Microsoft Visual C++.

int main() {
int something = {42}; // 1. LOC that makes me sick
// something = something; // To silent unused variable warning
}

{...} seems perfect for initialization for a structure or an array.
My question is; why is it valid to initialize an int with {42}?
Sometimes I think it is okay, but some other time this is confusing.
Please help.

Microsoft Visual C++ also compiles the following lines without any
complaints:
2. int weird[2] = {{{12}}};
3. int weirder = { { 3,} ,};

Victor Bazarov 11-02-2011 04:30 PM

Re: Why this LOC is valid <int something = {42};>
 
On 11/2/2011 9:30 AM, MJ_India wrote:
> C++ file with following code compiles without any complaint on GCC and
> Microsoft Visual C++.
>
> int main() {
> int something = {42}; // 1. LOC that makes me sick
> // something = something; // To silent unused variable warning
> }
>
> {...} seems perfect for initialization for a structure or an array.
> My question is; why is it valid to initialize an int with {42}?
> Sometimes I think it is okay, but some other time this is confusing.
> Please help.
>
> Microsoft Visual C++ also compiles the following lines without any
> complaints:
> 2. int weird[2] = {{{12}}};
> 3. int weirder = { { 3,} ,};


I think the curly braces are optional when writing the initialization of
a scalar variable. Commas are optional in some cases, but I am not sure
they are allowed in initializers; can simply be VC++ extension.

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

Jorgen Grahn 11-03-2011 02:08 PM

Re: Why this LOC is valid <int something = {42};>
 
On Wed, 2011-11-02, MJ_India wrote:
> C++ file with following code compiles without any complaint on GCC and
> Microsoft Visual C++.
>
> int main() {
> int something = {42}; // 1. LOC that makes me sick
> // something = something; // To silent unused variable warning
> }


Confirmed, with g++ and full warnings enabled. It must be standard C
and C++. That was news to me too, after ~20 years of C and C++
programming.

And yes, it makes me a bit sick too. On the other hand I have never
seen it cause any confusion in practice.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .


All times are GMT. The time now is 05:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.