From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from asav21.altibox.net (asav21.altibox.net [109.247.116.8]) by sourceware.org (Postfix) with ESMTPS id B162E393D023 for ; Thu, 4 Feb 2021 20:58:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B162E393D023 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hesbynett.no Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=david.brown@hesbynett.no Received: from mail.jansenbrown.no (unknown [92.221.34.247]) by asav21.altibox.net (Postfix) with ESMTP id E31D08015F; Thu, 4 Feb 2021 21:58:33 +0100 (CET) Received: from [192.168.4.227] (unicorn.lan [192.168.4.227]) by mail.jansenbrown.no (Postfix) with ESMTPSA id 86FFD215455; Thu, 4 Feb 2021 21:58:33 +0100 (CET) Subject: Re: Comma Operator - Left to Right Associativity To: AJ D , gcc@gcc.gnu.org References: From: David Brown Message-ID: <59add9a1-ef9b-1cde-a4b7-66a167595a6b@hesbynett.no> Date: Thu, 4 Feb 2021 21:58:33 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit X-CMAE-Score: 0 X-CMAE-Analysis: v=2.3 cv=Kb38TzQD c=1 sm=1 tr=0 a=+Fy6h7hJ4UJcWgHwdIx3jg==:117 a=+Fy6h7hJ4UJcWgHwdIx3jg==:17 a=IkcTkHD0fZMA:10 a=qa6Q16uM49sA:10 a=2QSLavsyAAAA:8 a=3g4sECMud5z2xuglxmwA:9 a=QEXdDO2ut3YA:10 a=9H_80fVQ3bbXSWzY4Kdq:22 X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, NICE_REPLY_A, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Feb 2021 20:58:38 -0000 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 (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