How is this use of curly braces working ?

Discussion in 'C++' started by Samkit Jain, Aug 10, 2010.

  1. Samkit Jain

    Samkit Jain Guest

    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
    Samkit Jain, Aug 10, 2010
    #1
    1. Advertising

  2. Samkit Jain

    Jonathan Lee Guest

    On Aug 10, 12:47 pm, Samkit Jain <> 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
    Jonathan Lee, Aug 10, 2010
    #2
    1. Advertising

  3. Samkit Jain

    Samkit Jain Guest

    On Aug 10, 10:55 pm, Jonathan Lee <> wrote:
    > On Aug 10, 12:47 pm, Samkit Jain <> 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.
    Samkit Jain, Aug 10, 2010
    #3
  4. Samkit Jain

    Samkit Jain Guest

    On Aug 10, 10:55 pm, Jonathan Lee <> wrote:
    > On Aug 10, 12:47 pm, Samkit Jain <> 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.
    Samkit Jain, Aug 10, 2010
    #4
  5. On 8/10/2010 2:38 PM, Samkit Jain wrote:
    > On Aug 10, 10:55 pm, Jonathan Lee<> wrote:
    >> On Aug 10, 12:47 pm, Samkit Jain<> 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
    Victor Bazarov, Aug 10, 2010
    #5
  6. Samkit Jain

    Kai-Uwe Bux Guest

    Victor Bazarov wrote:

    > On 8/10/2010 2:38 PM, Samkit Jain wrote:
    >> On Aug 10, 10:55 pm, Jonathan Lee<> wrote:
    >>> On Aug 10, 12:47 pm, Samkit Jain<> 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 Bux, Aug 10, 2010
    #6
  7. Samkit Jain

    Samkit Jain Guest

    On Aug 10, 11:57 pm, Kai-Uwe Bux <> wrote:
    > Victor Bazarov wrote:
    > > On 8/10/2010 2:38 PM, Samkit Jain wrote:
    > >> On Aug 10, 10:55 pm, Jonathan Lee<>  wrote:
    > >>> On Aug 10, 12:47 pm, Samkit Jain<>  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;} ?
    Samkit Jain, Aug 11, 2010
    #7
  8. Samkit Jain

    Ian Collins Guest

    On 08/11/10 02:28 PM, Samkit Jain wrote:
    > On Aug 10, 11:57 pm, Kai-Uwe Bux<> 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
    Ian Collins, Aug 11, 2010
    #8
  9. Samkit Jain

    Jonathan Lee Guest

    On Aug 10, 9:28 pm, Samkit Jain <> 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
    Jonathan Lee, Aug 11, 2010
    #9
  10. Samkit Jain

    Jonathan Lee Guest

    On Aug 10, 10:24 pm, Jonathan Lee <> wrote:
    > On Aug 10, 9:28 pm, Samkit Jain <> 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
    Jonathan Lee, Aug 11, 2010
    #10
  11. Samkit Jain wrote:
    > ...
    > 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.

    --
    Bets regards,
    Andrey Tarasevich
    Andrey Tarasevich, Aug 12, 2010
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dustin D.
    Replies:
    0
    Views:
    2,300
    Dustin D.
    Aug 27, 2003
  2. Geathaa
    Replies:
    1
    Views:
    534
    Martin Honnen
    Sep 12, 2003
  3. TJ
    Replies:
    5
    Views:
    569
  4. Replies:
    10
    Views:
    771
    John Machin
    May 17, 2009
  5. Ken Fine
    Replies:
    2
    Views:
    173
    Ken Fine
    Feb 24, 2004
Loading...

Share This Page