public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Comma Operator - Left to Right Associativity
@ 2021-02-04 20:08 AJ D
  2021-02-04 20:58 ` David Brown
  0 siblings, 1 reply; 6+ messages in thread
From: AJ D @ 2021-02-04 20:08 UTC (permalink / raw)
  To: gcc

Isn't comma operator suppose to honor left-to-right associativity?

When I try it on this test case, it exhibits right-to-left associativity.

#include <iostream>



struct CBI;

struct EC;

struct CET;



struct CBI {

    CBI& operator,(const CBI& rhs)

        { return *const_cast<CBI*>(&rhs); }

};



struct EC : CBI {

    explicit EC(CET* cet) : cet_(cet)

        {}



    CET* cet_;

};



struct CET {

    CBI& operator,(const CBI& rhs) const

        { return *const_cast<CBI*>(&rhs); }



    operator EC&() const

        { return *new EC(const_cast<CET*>(this)); }

};



static const CET&

hello() {

    std::cout << "Hello " << std::endl;

    return *new CET();

}



static const CET&

world() {

    std::cout << "World " << std::endl;

    return *new CET();

}



static void

test_comma_operator(CBI&) {



}



int main()

{

    test_comma_operator ((

        hello(),

        world()

    ));

}



CLANG appears to do it right.


us01odcvde08782> clang++ -g test.cpp

us01odcvde08782> ./a.out

*Hello*

World

us01odcvde08782> g++ -g test.cpp

us01odcvde08782> ./a.out

World

*Hello*

us01odcvde08782>


I was using CentOS6.8 with gcc 6.2. However, trying other versions of GCC
didn't make any difference.


Is this a bug in GCC?

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-02-04 22:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-04 20:08 Comma Operator - Left to Right Associativity AJ D
2021-02-04 20:58 ` David Brown
2021-02-04 21:21   ` Andreas Schwab
2021-02-04 21:33     ` David Brown
2021-02-04 22:46       ` AJ D
2021-02-04 22:51         ` AJ D

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).