Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7634
| From | Kai-Uwe Bux <jkherciueh@gmx.net> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Templated Casting operators |
| Followup-To | comp.lang.c++ |
| Date | 2011-07-08 09:42 +0200 |
| Organization | University of Bielefeld |
| Message-ID | <iv6cds$a0f$1@hoshi.visyn.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> |
Followups directed to: comp.lang.c++
Narinder wrote:
[...]
> Consider 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&()const
> {
> cout << "I am CONST version\n";
> return pi;
> }
>
>
> };
>
> template<class T>
> T& show(T &t)
> {
> cout << "NON-CONST show\n";
> return t;
> }
>
> template<class T>
> const T& show(const T &t)
> {
> cout << "CONST show\n";
> return t;
> }
>
> int main()
> {
>
> klass k;
> show(k);
> show(klass());
>
> double x1 = k;
> double x2 = klass();
>
> }
>
> -----------------------------------------------------
>
> Output from msvc :
> NON-CONST show
> CONST show
> I am CONST version
> I am CONST version
>
> Output from gcc
> NON-CONST show
> CONST show
> I am NON-CONST version
> I am NON-CONST version
>
> To me the msvc appears 'correct' or at least self-consistent. Or
> perhaps I am missing something.
>
> Any thoughts ?
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
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