Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7733

Re: Slicing with ?: operator - expected?

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Slicing with ?: operator - expected?
Date 2011-07-11 13:05 -0400
Organization A noiseless patient Spider
Message-ID <ivfagb$7p5$1@dont-email.me> (permalink)
References <iv1p6s$acb$1@dont-email.me> <Xns9F1AAF6315183jgreerdoubletakecom@88.198.244.100> <iv7ndt$f33$1@dont-email.me> <Xns9F1F801A05D6jgreerdoubletakecom@88.198.244.100>

Show all headers | View raw


On 7/11/2011 12:35 PM, Joe Greer wrote:
> Victor Bazarov<v.bazarov@comcast.invalid>  wrote in
> news:iv7ndt$f33$1@dont-email.me:
>
>> On 7/6/2011 5:14 PM, Joe Greer wrote:
>>> Victor Bazarov<v.bazarov@comcast.invalid>   wrote in
>>> news:iv1p6s$acb$1@dont- email.me:
>>>
>>>>
>>>>       void foo(const Foo* pFoo)
>>>>       {
>>>>          (pFoo ? *pFoo : Foo1()).bar(); // line 19 ************
>>>>       }
>>>>
>>>
>>> I wonder if it isn't the *pFoo that is doing the slicing since we are
>>> dealing with a type const Foo* in the argument list.
>>
>> Doing slicing where?  On its way into the 'foo' function?  Certainly
>> not.  If I rewrite the ?: with 'if-else' syntax, I get the dynamic
>> dispatch.
>>
>> V
>
> [expr.cond]/3 applies in the case of a condition expression whereas it
> doesn't for the if-else stmt.  The text is:
>
>      3 Otherwise, if the second and third operand have different types,
>      and either has (possibly cv-qualified) class type, an attempt is made
>      to convert each of those operands to the type of the other. The
>      process for determining whether an operand expression E1 of type T1
>      can be converted to match an operand expression E2 of type T2 is
>      defined as follows:
>         — If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
>           implicitly converted (clause 4) to the type “reference to T2”,
>           subject to the constraint that in the conversion the reference
>           must bind directly (8.5.3) to E1.
>
>         — If E2 is an rvalue, or if the conversion above cannot be done:
>            — if E1 and E2 have class type, and the underlying class types
>              are the same or one is a base class of the other: E1 can be
>              converted to match E2 if the class of T2 is the same type as,
>              or a base class of, the class of T1, and the cv-qualification
>              of T2 is the same cv-qualification as, or a greater cv-
>              qualification than, the cv-qualification of T1. If the
>              conversion is applied, E1 is changed to an rvalue of type T2
>              that still refers to the original source class object (or the
>              appropriate subobject thereof). [Note: that is, no copy is
>              made. ]
>
> The way I read it is that since Foo1() is convertible to Foo, but not the
> otherway around, Foo1() will be converted/sliced to Foo then the bar()
> function will be called.

I think I get what you're saying.  In this particular case, since 'pFoo' 
was not null, the second part (E2) wasn't even evaluated.  The main 
driving thing in your analysis is that the common type is 'const Foo' 
(and not 'const Foo&' like one compiler used to think).  And it really 
comes from the definition of 'operator*' (earlier in the chapter 5).

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Slicing with ?: operator - expected? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-06 09:50 -0400
  Re: Slicing with ?: operator - expected? Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-06 07:41 -0700
    Re: Slicing with ?: operator - expected? Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-06 07:50 -0700
  Re: Slicing with ?: operator - expected? Bart van Ingen Schenau <bart@ingen.ddns.info> - 2011-07-06 16:41 +0200
    Re: Slicing with ?: operator - expected? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-06 11:37 -0400
      Re: Slicing with ?: operator - expected? "Balog Pal" <pasa@lib.hu> - 2011-07-06 23:17 +0200
  Re: Slicing with ?: operator - expected? Joe Greer <jgreer@doubletake.com> - 2011-07-06 21:14 +0000
    Re: Slicing with ?: operator - expected? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-08 15:56 -0400
      Re: Slicing with ?: operator - expected? Joe Greer <jgreer@doubletake.com> - 2011-07-11 16:35 +0000
        Re: Slicing with ?: operator - expected? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-11 13:05 -0400
  Re: Slicing with ?: operator - expected? "Paul" <pchristor@yahoo.co.uk> - 2011-07-07 01:47 +0100

csiph-web