Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7637
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Templated Casting operators |
| Date | 2011-07-08 20:23 +1200 |
| Message-ID | <97nt5kF6tbU4@mid.individual.net> (permalink) |
| References | <8890cd86-3471-4b20-816a-82e27d2ebed0@d7g2000vbv.googlegroups.com> <iv5ko1$3qb$1@news.albasani.net> <8edcf612-c6e3-492b-b923-d418a9b8a1ab@s17g2000yqs.googlegroups.com> <iv6cds$a0f$1@hoshi.visyn.net> <da0363c1-396f-4bf2-87c3-38f3121bf3f4@a31g2000vbt.googlegroups.com> |
On 07/ 8/11 08:16 PM, Narinder wrote:
> On Jul 8, 8:42 am, Kai-Uwe Bux<jkherci...@gmx.net> wrote:
>>
>> I think, gcc gets it right and lets you observe a language quirk: In this
>> version, the two conversion operators differ by constness. Thus, overload
>> resolution should be based on constness of the klass object being converted:
>>
>> klass k; // declares a non-const klass object k
>> show(k); // should choose the T& version
>> show( klass() ); // should choose the T const& version because of 8.5.3/5
>> double x1 = k; // should choose the non-const member as k is non-const
>> double x2 = klass(); // should choose the non-const member as the
>> // temporary klass() is non-const
>>
>> The last issue is an inconsistency in the language: though klass() is non-
>> const, the temporary cannot be used to initialize a non-const reference.
>>
>> Best,
>>
>> Kai-Uwe Bux
>
> Hi
> Thanks for the reply.
>
> I was under the impress that a temporary variable would be cast only
> as a
> const&, which would make sense.
>
> I find the output of the following code :
>
> --------------------------------------------------------------
> #include<iostream>
> using namespace std;
>
> double pi = 3.142;
> struct klass
> {
>
> template<class T>
> operator T&()
> {
> cout<< "I am NON-const version\n";
> return pi;
> }
>
> template<class T>
> operator const T&()
> {
> cout<< "I am CONST version\n";
> return pi;
> }
> };
>
> int main()
> {
>
> klass k;
> double&x = k;
> const double&const_x = k;
>
> }
>
> -------------------------------------------------------------
>
>
> more 'intuitivley' correct with msvc.
> I am NON-const version
> I am CONST version
>
>
> than gcc
> I am NON-const version
> I am NON-const version
I think gcc is correct because k is non-const.
With gcc 4.5.1, I get
I am NON-const version
I am CONST version
So there must have been a recent change.
--
Ian Collins
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-07 16:34 -0700
Re: Templated Casting operators Alexander Bartolich <alexander.bartolich@gmx.at> - 2011-07-08 00:58 +0000
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-07 22:36 -0700
Re: Templated Casting operators Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-07-08 09:42 +0200
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-08 01:16 -0700
Re: Templated Casting operators Ian Collins <ian-news@hotmail.com> - 2011-07-08 20:23 +1200
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-08 01:37 -0700
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-08 06:28 -0700
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-08 01:40 -0700
Re: Templated Casting operators Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-07-09 00:06 +0200
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-09 03:16 -0700
Re: Templated Casting operators Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-07-09 13:34 +0200
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-09 07:18 -0700
Re: Templated Casting operators Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-07-10 01:15 +0200
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-09 21:39 -0700
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-09 22:01 -0700
Re: Templated Casting operators Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-07-10 13:43 +0200
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-10 05:55 -0700
Re: Templated Casting operators Narinder <narinder.claire@gmail.com> - 2011-07-10 00:19 -0700
Re: Templated Casting operators Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-07-10 12:03 +0200
csiph-web