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


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

Re: Templated Casting operators

From Kai-Uwe Bux <jkherciueh@gmx.net>
Newsgroups comp.lang.c++
Subject Re: Templated Casting operators
Followup-To comp.lang.c++
Date 2011-07-09 00:06 +0200
Organization University of Bielefeld
Message-ID <iv7v24$oo9$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> <iv6cds$a0f$1@hoshi.visyn.net> <8aed9603-26d2-41fb-b197-213bdf90ff99@34g2000yqr.googlegroups.com>

Followups directed to: comp.lang.c++

Show all headers | View raw


Narinder wrote:

> On Jul 8, 8:42 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
>> 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
> 
> Hi,
> 
> Thanks for the reply,
> 
> For 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;
> double x2 = klass();
> 
> }
> 
> ---------------------------------
> 
> msvc =>
> I am CONST version
> I am CONST version
> 
> and gcc =>
> I am NON-const version
> I am NON-const version
> 
> Which one would you say adhere's to language correctness and which one
> is
> desired ?

I would expect a compilation error due to ambiguity. The code seems to be 
just a template version of something like this:

class k {

  double value;

public:

  k ( double v )
    : value ( v )
  {}

  operator double & ( void ) {
    std::cout << "non-const\n";
    return ( value );
  }

  operator double const & ( void ) {
    std::cout << "const\n";
    return ( value );
  }

};

int main ( void ) {
  k x ( 13.4 );
  double a = x;
  double const b = x;
}

Here, you get a compilation error. I wonder why you don't with templates: 
even Comeau compiles the template version. From the test case

#include <iostream>

template < typename T >
struct oops;

class k {

  double value;

public:

  k ( double v )
    : value ( v )
  {}

  template < typename T >
  operator T & ( void ) {
    std::cout << "non-const\n";
    return ( value );
  }

  template < typename T >
  operator T const & ( void ) {
    oops<T>::value;
    std::cout << "const\n";
    return ( value );
  }

};

int main ( void ) {
  k x ( 13.4 );
  double a = x;
  double const b = x;
}

and the version, where oops<T>::value is placed into the non-const version, 
one can see that Comeau (like gcc) instantiates only the operator T& 
template. At the moment, I don't see why.


Best,

Kai-Uwe Bux

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


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