Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why this LOC is valid <int something = {42};>

Reply
Thread Tools

Why this LOC is valid <int something = {42};>

 
 
MJ_India
Guest
Posts: n/a
 
      11-02-2011
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,} ,};
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-02-2011
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
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-03-2011
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 .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MovieBuffAlert:LOC Archive Moving Curtin/Dobbs DVD Video 0 01-26-2007 06:30 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
LOC in Python and C++ in large projects Adriaan Renting Python 1 07-01-2005 03:17 PM
not able to write to addr loc x0 fpgawizz VHDL 1 04-02-2005 05:55 PM
Eclipse question regarding LOC. El Durango Java 4 07-20-2004 01:00 AM



Advertisments