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

* Re: Comma Operator - Left to Right Associativity
  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
  0 siblings, 1 reply; 6+ messages in thread
From: David Brown @ 2021-02-04 20:58 UTC (permalink / raw)
  To: AJ D, gcc

On 04/02/2021 21:08, AJ D via Gcc wrote:
> 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.

You are not talking about associativity - you are talking about
evaluation order.  (The two things are often mixed up.)

For the built-in comma operator, you get guaranteed order of evaluation
(or more precisely, guaranteed order of visible side-effects).  But for
a user-defined comma operator, you do not - until C++17, which has
guaranteed evaluation ordering in some circumstances.

See <https://en.cppreference.com/w/cpp/language/operator_other> (it's
easier to read than the C++ standards).

Try your test again with "-std=c++17" or "-std=g++17" - if the order is
still reversed, it's a gcc bug (AFAICS).  But for standards prior to
C++17, the ordering is unspecified for user-defined comma operators.

This is not unlike the difference between the built-in logic operators
&& and ||, which are guaranteed short-circuiting, while user-defined
overloads are not.  And for arithmetic operators, you don't get integer
promotion, automatic conversion to a common type, etc.

Basically, the user-defined operators are just syntactic sugar for a
function call - they don't have the "magic" features of the real operators.

David


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

* Re: Comma Operator - Left to Right Associativity
  2021-02-04 20:58 ` David Brown
@ 2021-02-04 21:21   ` Andreas Schwab
  2021-02-04 21:33     ` David Brown
  0 siblings, 1 reply; 6+ messages in thread
From: Andreas Schwab @ 2021-02-04 21:21 UTC (permalink / raw)
  To: David Brown; +Cc: AJ D, gcc

On Feb 04 2021, David Brown wrote:

> For the built-in comma operator, you get guaranteed order of evaluation
> (or more precisely, guaranteed order of visible side-effects).  But for
> a user-defined comma operator, you do not - until C++17, which has
> guaranteed evaluation ordering in some circumstances.

But not the evaluation order of function arguments.  See
<https://en.cppreference.com/w/cpp/language/eval_order> Sequenced-before
rules, rule 15.

> Try your test again with "-std=c++17" or "-std=g++17" - if the order is
> still reversed, it's a gcc bug (AFAICS).

I don't think so.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

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

* Re: Comma Operator - Left to Right Associativity
  2021-02-04 21:21   ` Andreas Schwab
@ 2021-02-04 21:33     ` David Brown
  2021-02-04 22:46       ` AJ D
  0 siblings, 1 reply; 6+ messages in thread
From: David Brown @ 2021-02-04 21:33 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: AJ D, gcc



On 04/02/2021 22:21, Andreas Schwab wrote:
> On Feb 04 2021, David Brown wrote:
> 
>> For the built-in comma operator, you get guaranteed order of evaluation
>> (or more precisely, guaranteed order of visible side-effects).  But for
>> a user-defined comma operator, you do not - until C++17, which has
>> guaranteed evaluation ordering in some circumstances.
> 
> But not the evaluation order of function arguments.  See
> <https://en.cppreference.com/w/cpp/language/eval_order> Sequenced-before
> rules, rule 15.

Correct.

> 
>> Try your test again with "-std=c++17" or "-std=g++17" - if the order is
>> still reversed, it's a gcc bug (AFAICS).
> 
> I don't think so.
> 

Unless I am missing something, in the OP's program it is a user-defined
comma operator that is called.  There is only one argument to the
"test_comma_operator" function, the result of that user-defined comma
operator.  So rule 15 above does not apply - rule 16 applies.

At least that is /my/ reading of the cppreference page and the OP's program.

David


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

* Re: Comma Operator - Left to Right Associativity
  2021-02-04 21:33     ` David Brown
@ 2021-02-04 22:46       ` AJ D
  2021-02-04 22:51         ` AJ D
  0 siblings, 1 reply; 6+ messages in thread
From: AJ D @ 2021-02-04 22:46 UTC (permalink / raw)
  To: David Brown; +Cc: Andreas Schwab, gcc

Nope, -std=c++17 didn’t help either.

On Thu, Feb 4, 2021 at 1:33 PM David Brown <david.brown@hesbynett.no> wrote:

>
>
> On 04/02/2021 22:21, Andreas Schwab wrote:
> > On Feb 04 2021, David Brown wrote:
> >
> >> For the built-in comma operator, you get guaranteed order of evaluation
> >> (or more precisely, guaranteed order of visible side-effects).  But for
> >> a user-defined comma operator, you do not - until C++17, which has
> >> guaranteed evaluation ordering in some circumstances.
> >
> > But not the evaluation order of function arguments.  See
> > <https://en.cppreference.com/w/cpp/language/eval_order> Sequenced-before
> > rules, rule 15.
>
> Correct.
>
> >
> >> Try your test again with "-std=c++17" or "-std=g++17" - if the order is
> >> still reversed, it's a gcc bug (AFAICS).
> >
> > I don't think so.
> >
>
> Unless I am missing something, in the OP's program it is a user-defined
> comma operator that is called.  There is only one argument to the
> "test_comma_operator" function, the result of that user-defined comma
> operator.  So rule 15 above does not apply - rule 16 applies.
>
> At least that is /my/ reading of the cppreference page and the OP's
> program.
>
> David
>
>

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

* Re: Comma Operator - Left to Right Associativity
  2021-02-04 22:46       ` AJ D
@ 2021-02-04 22:51         ` AJ D
  0 siblings, 0 replies; 6+ messages in thread
From: AJ D @ 2021-02-04 22:51 UTC (permalink / raw)
  To: David Brown; +Cc: Andreas Schwab, gcc

Yes, there is only function argument which is the result of the comma
operator.

On Thu, Feb 4, 2021 at 2:46 PM AJ D <aatsnps@gmail.com> wrote:

> Nope, -std=c++17 didn’t help either.
>
> On Thu, Feb 4, 2021 at 1:33 PM David Brown <david.brown@hesbynett.no>
> wrote:
>
>>
>>
>> On 04/02/2021 22:21, Andreas Schwab wrote:
>> > On Feb 04 2021, David Brown wrote:
>> >
>> >> For the built-in comma operator, you get guaranteed order of evaluation
>> >> (or more precisely, guaranteed order of visible side-effects).  But for
>> >> a user-defined comma operator, you do not - until C++17, which has
>> >> guaranteed evaluation ordering in some circumstances.
>> >
>> > But not the evaluation order of function arguments.  See
>> > <https://en.cppreference.com/w/cpp/language/eval_order>
>> Sequenced-before
>> > rules, rule 15.
>>
>> Correct.
>>
>> >
>> >> Try your test again with "-std=c++17" or "-std=g++17" - if the order is
>> >> still reversed, it's a gcc bug (AFAICS).
>> >
>> > I don't think so.
>> >
>>
>> Unless I am missing something, in the OP's program it is a user-defined
>> comma operator that is called.  There is only one argument to the
>> "test_comma_operator" function, the result of that user-defined comma
>> operator.  So rule 15 above does not apply - rule 16 applies.
>>
>> At least that is /my/ reading of the cppreference page and the OP's
>> program.
>>
>> David
>>
>>

^ 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).