220 15965 <CAFk2RUbDL_BedgBP_688mThXS2FfdZP=vyBjUHatvyW5qYvGEQ@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Ville Voutilainen <ville.voutilainen@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: const T* operator->() const
Date: Mon, 2 Feb 2015 16:39:47 +0200
Lines: 69
Approved: news@gmane.org
Message-ID: <CAFk2RUbDL_BedgBP_688mThXS2FfdZP=vyBjUHatvyW5qYvGEQ@mail.gmail.com>
References: <56a7727d-9839-45c9-be72-a6ecb61f38c9@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
X-Trace: ger.gmane.org 1422888001 7446 80.91.229.3 (2 Feb 2015 14:40:01 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 2 Feb 2015 14:40:01 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBC5JHI7A7ALRBNEYX2TAKGQEHDRUXIA@isocpp.org Mon Feb 02 15:40:01 2015
Return-path: <std-proposals+bncBC5JHI7A7ALRBNEYX2TAKGQEHDRUXIA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pa0-f70.google.com ([209.85.220.70])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBC5JHI7A7ALRBNEYX2TAKGQEHDRUXIA@isocpp.org>)
	id 1YIIAS-0001m0-0J
	for gclcip-std-proposals@m.gmane.org; Mon, 02 Feb 2015 15:40:00 +0100
Original-Received: by mail-pa0-f70.google.com with SMTP id eu11sf458461123pac.1
        for <gclcip-std-proposals@m.gmane.org>; Mon, 02 Feb 2015 06:39:48 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:date
         :message-id:subject:from:to:content-type:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=GXbnxDev1d8t7MyY4bfCWKHvxNhyKhMKOf1Ho42xR7M=;
        b=ePGStjeMZgToDwgJK/3i85L6oobw/2eQNPZd8264HwHX+k3fLrH7RqrPYPf+rNQSjt
         JeG6S1YiG0wqyd/Fdhx0PILHOW9PBCVpEhPRlkQfKtIJaK7Z4IAU+b5bCHnQdVKiOw+y
         BBrG2vyOiwfD9JNOskiKyMf8253s0DAn6yBGaJI7BefrzxFIQd50AYeV1pYTdySCDJid
         f9Qp6duReXQQM3fqy3mNT7rvRIJp91Mnxh11aR4t9Ikzmxppkne+y3Ny4Nu5M3sIlARp
         Acs0fbHRzrXE7a5LJQgiGS9O+GYo4qhf/VetXuBrsZN229YDU4pAuV+8FlbLQf59rz5T
         T/vg==
X-Gm-Message-State: ALoCoQnhzVTNNTC5jhCNcA26RNQt2L14IE003dKSHQXqca/iCMGkZhPLdBjBreocKfp9fRKbHvbO
X-Received: by 10.66.149.170 with SMTP id ub10mr17356676pab.15.1422887988935;
        Mon, 02 Feb 2015 06:39:48 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.148.85 with SMTP id w82ls1905699iod.100.gmail; Mon, 02 Feb
 2015 06:39:47 -0800 (PST)
X-Received: by 10.107.158.146 with SMTP id h140mr22431529ioe.27.1422887987923;
        Mon, 02 Feb 2015 06:39:47 -0800 (PST)
Original-Received: from mail-ig0-x22b.google.com (mail-ig0-x22b.google.com. [2607:f8b0:4001:c05::22b])
        by mx.google.com with ESMTPS id bs1si7569645icb.34.2015.02.02.06.39.47
        for <std-proposals@isocpp.org>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Mon, 02 Feb 2015 06:39:47 -0800 (PST)
Received-SPF: pass (google.com: domain of ville.voutilainen@gmail.com designates 2607:f8b0:4001:c05::22b as permitted sender) client-ip=2607:f8b0:4001:c05::22b;
Original-Received: by mail-ig0-f171.google.com with SMTP id h15so2354751igd.4
        for <std-proposals@isocpp.org>; Mon, 02 Feb 2015 06:39:47 -0800 (PST)
X-Received: by 10.107.36.70 with SMTP id k67mr19997915iok.30.1422887987827;
 Mon, 02 Feb 2015 06:39:47 -0800 (PST)
Original-Received: by 10.36.0.4 with HTTP; Mon, 2 Feb 2015 06:39:47 -0800 (PST)
In-Reply-To: <56a7727d-9839-45c9-be72-a6ecb61f38c9@isocpp.org>
X-Original-Sender: ville.voutilainen@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of ville.voutilainen@gmail.com designates 2607:f8b0:4001:c05::22b as
 permitted sender) smtp.mail=ville.voutilainen@gmail.com;       dkim=pass
 header.i=@gmail.com;       dmarc=pass (p=NONE dis=NONE) header.from=gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:15965
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/15965>

On 2 February 2015 at 16:27, mobiphil <mobi@mobiphil.com> wrote:
> Problem: compiler does not paste const operator -> when used to refer to a
> const method
> Proposal: well, fix it
>
> Context: Thought a trivial solution for cow, but does not work. The idea was
> to have a Cow wrapper template around the classes used in my system. The
> protocol would be: if a non const method is called on the object then a copy
> is made (if necessary), otherwise just call the method. The idea was to play
> with pair of const and nonconst operator->
>
> So, in the example below, I thought for a moment that when writing
> a->constFunc() for non const object the const "->" operator would be chosen
> by the compiler as constFunc() is const, but had to conclude that it is not
> the case.
>
> What is your opinion? Was my expectation completely wrong. If was not wrong,
> could this make subject of a proposal?
>
> thanks for your feedback
>
> #include <cstdio>
>
> template<class T>
> class Cow {
>    private:
>       T* ptr;
>    public:
>
>       T* operator->() {
>          printf("-> called\n")  ;
>       }
>       const T* operator->() const {
>          printf("const -> called\n")  ;
>       }
> };
>
> class A {
>    public:
>       void func() {};
>       void constFunc() const  {};
> };
>
>
> int main()
> {
>    Cow<A> a = Cow<A>();
>    a->constFunc(); /* as constFunc is const, would expect the operator->
> const to be used*/
>
>    const Cow<A> ca = Cow<A>();
>    ca->constFunc(); /* well, this one uses the const operator -> */
> }


Your expectation is wrong. What the result of an operator-> is used to
access doesn't
affect the overload resolution of operator-> itself, so the first case
with a non-const Cow<A>
will use the non-const Cow<A>::operator->.

-- 

--- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

.
