How is this use of curly braces working ?

S

Samkit Jain

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
 
J

Jonathan Lee

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
 
S

Samkit Jain

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.
 
S

Samkit Jain

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.
 
K

Kai-Uwe Bux

Victor said:
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
 
S

Samkit Jain

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;} ?
 
J

Jonathan Lee

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
 
J

Jonathan Lee

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
 
A

Andrey Tarasevich

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

They are _not_ "working", as far as C++ language is concerned. Your code
is invalid, ill-formed.

What you have above is a well-known extension of GCC compiler, called
"statement expressions". It is specific to that compiler. It has nothing
to do with C++ language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top