Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How is this use of curly braces working ?

Reply
Thread Tools

How is this use of curly braces working ?

 
 
Samkit Jain
Guest
Posts: n/a
 
      08-10-2010
Code:
--------
#include <iostream>

using namespace std;

class Parent
{
public:
Parent()
{
cout << "Constructing " << this<< endl;
}

Parent(Parent& another)
{
cout << "Constructing (copy) " << this << endl;
}

~Parent()
{
cout << "Destructing " << this << endl;
}

void print()
{
cout << "Hello world" << endl;
}
};

int main()
{
Parent object;
({ object; }).print();
return 0;
}


Output:
-----------
Constructing 0xbfad96bf
Constructing (copy) 0xbfad96be
Hello world
Destructing 0xbfad96be
Destructing 0xbfad96bf
 
Reply With Quote
 
 
 
 
Jonathan Lee
Guest
Posts: n/a
 
      08-10-2010
On Aug 10, 12:47*pm, Samkit Jain <(E-Mail Removed)> wrote:
> int main()
> {
> * * * * Parent object;
> * * * * ({ object; }).print();
> * * * * return 0;
>
> }


Probably as an extension provided by your compiler. For example,
when compiled with g++ with -pedantic I get a warning:

ISO C++ forbids braced-groups within expressions

At least some other languages support this kind of thing
(ex., I think Javascript).

--Jonathan
 
Reply With Quote
 
 
 
 
Samkit Jain
Guest
Posts: n/a
 
      08-10-2010
On Aug 10, 10:55*pm, Jonathan Lee <(E-Mail Removed)> wrote:
> On Aug 10, 12:47*pm, Samkit Jain <(E-Mail Removed)> wrote:
>
> > int main()
> > {
> > * * * * Parent object;
> > * * * * ({ object; }).print();
> > * * * * return 0;

>
> > }

>
> Probably as an extension provided by your compiler. For example,
> when compiled with g++ with -pedantic I get a warning:
>
> * ISO C++ forbids braced-groups within expressions
>
> At least some other languages support this kind of thing
> (ex., I think Javascript).
>
> --Jonathan


I am able to compile it on g++ 3.4.6 as well as on 4.4.3.
 
Reply With Quote
 
Samkit Jain
Guest
Posts: n/a
 
      08-10-2010
On Aug 10, 10:55*pm, Jonathan Lee <(E-Mail Removed)> wrote:
> On Aug 10, 12:47*pm, Samkit Jain <(E-Mail Removed)> wrote:
>
> > int main()
> > {
> > * * * * Parent object;
> > * * * * ({ object; }).print();
> > * * * * return 0;

>
> > }

>
> Probably as an extension provided by your compiler. For example,
> when compiled with g++ with -pedantic I get a warning:
>
> * ISO C++ forbids braced-groups within expressions
>
> At least some other languages support this kind of thing
> (ex., I think Javascript).
>
> --Jonathan


I am able to compile it on g++ 3.4.6 and g++ 4.4.3.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-10-2010
On 8/10/2010 2:38 PM, Samkit Jain wrote:
> On Aug 10, 10:55 pm, Jonathan Lee<(E-Mail Removed)> wrote:
>> On Aug 10, 12:47 pm, Samkit Jain<(E-Mail Removed)> wrote:
>>
>>> int main()
>>> {
>>> Parent object;
>>> ({ object; }).print();
>>> return 0;

>>
>>> }

>>
>> Probably as an extension provided by your compiler. For example,
>> when compiled with g++ with -pedantic I get a warning:
>>
>> ISO C++ forbids braced-groups within expressions
>>
>> At least some other languages support this kind of thing
>> (ex., I think Javascript).
>>
>> --Jonathan

>
> I am able to compile it on g++ 3.4.6 and g++ 4.4.3.


With "-pedantic"?

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-10-2010
Victor Bazarov wrote:

> On 8/10/2010 2:38 PM, Samkit Jain wrote:
>> On Aug 10, 10:55 pm, Jonathan Lee<(E-Mail Removed)> wrote:
>>> On Aug 10, 12:47 pm, Samkit Jain<(E-Mail Removed)> wrote:
>>>
>>>> int main()
>>>> {
>>>> Parent object;
>>>> ({ object; }).print();
>>>> return 0;
>>>
>>>> }
>>>
>>> Probably as an extension provided by your compiler. For example,
>>> when compiled with g++ with -pedantic I get a warning:
>>>
>>> ISO C++ forbids braced-groups within expressions
>>>
>>> At least some other languages support this kind of thing
>>> (ex., I think Javascript).
>>>
>>> --Jonathan

>>
>> I am able to compile it on g++ 3.4.6 and g++ 4.4.3.

>
> With "-pedantic"?


Yes, you get a warning but the compiler still goes ahead and generated an
executable.

Nonetheless, it's non-conforming code (compiled only via an extension) and
g++ says so.


Best

Kai-Uwe Bux

 
Reply With Quote
 
Samkit Jain
Guest
Posts: n/a
 
      08-11-2010
On Aug 10, 11:57*pm, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> Victor Bazarov wrote:
> > On 8/10/2010 2:38 PM, Samkit Jain wrote:
> >> On Aug 10, 10:55 pm, Jonathan Lee<(E-Mail Removed)> *wrote:
> >>> On Aug 10, 12:47 pm, Samkit Jain<(E-Mail Removed)> *wrote:

>
> >>>> int main()
> >>>> {
> >>>> * * * * *Parent object;
> >>>> * * * * *({ object; }).print();
> >>>> * * * * *return 0;

>
> >>>> }

>
> >>> Probably as an extension provided by your compiler. For example,
> >>> when compiled with g++ with -pedantic I get a warning:

>
> >>> * *ISO C++ forbids braced-groups within expressions

>
> >>> At least some other languages support this kind of thing
> >>> (ex., I think Javascript).

>
> >>> --Jonathan

>
> >> I am able to compile it on g++ 3.4.6 and g++ 4.4.3.

>
> > With "-pedantic"?

>
> Yes, you get a warning but the compiler still goes ahead and generated an
> executable.
>
> Nonetheless, it's non-conforming code (compiled only via an extension) and
> g++ says so.
>
> Best
>
> Kai-Uwe Bux


Kai-Uwe,
That may be right (I am not sure).
What do we call this kind of operator/semantic as, like {obj;} ?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-11-2010
On 08/11/10 02:28 PM, Samkit Jain wrote:
> On Aug 10, 11:57 pm, Kai-Uwe Bux<(E-Mail Removed)> wrote:
>>
>> Nonetheless, it's non-conforming code (compiled only via an extension) and
>> g++ says so.

>
> Kai-Uwe,
> That may be right (I am not sure).
> What do we call this kind of operator/semantic as, like {obj;} ?


A gcc extension.

--
Ian Collins
 
Reply With Quote
 
Jonathan Lee
Guest
Posts: n/a
 
      08-11-2010
On Aug 10, 9:28*pm, Samkit Jain <(E-Mail Removed)> wrote:
> Kai-Uwe,
> That may be right (I am not sure).
> What do we call this kind of operator/semantic as, like {obj;} ?


I think "compound statement" would be the term used by other
languages. C++ does not call it anything (I mean, of course, when
there is some sort of "return value").

As an extension, however, don't expect it to compile with other
build systems.

--Jonathan
 
Reply With Quote
 
Jonathan Lee
Guest
Posts: n/a
 
      08-11-2010
On Aug 10, 10:24*pm, Jonathan Lee <(E-Mail Removed)> wrote:
> On Aug 10, 9:28*pm, Samkit Jain <(E-Mail Removed)> wrote:
>
> > Kai-Uwe,
> > That may be right (I am not sure).
> > What do we call this kind of operator/semantic as, like {obj;} ?

>
> I think "compound statement" would be the term used by other
> languages. C++ does not call it anything (I mean, of course, when
> there is some sort of "return value").
>
> As an extension, however, don't expect it to compile with other
> build systems.
>
> --Jonathan


Nevermind.. Looks like that's a fairly common synonym for what
I would call a block statement ("return value" or not).

--Jonathan
 
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
Macro substitution: curly braces behaviour? TJ C Programming 5 02-14-2007 11:49 AM
XMLHTTP: translating entities like curly quotes and curly apostrophes? Ken Fine ASP General 2 02-24-2004 11:17 AM
...curly problem for a curly guru philpot Computer Support 5 11-29-2003 12:02 PM
AVT / Curly braces : why don't they work for me ? Geathaa XML 1 09-12-2003 01:35 PM
matching curly braces and regular expressions Dustin D. Perl 0 08-26-2003 11:18 PM



Advertisments