Topic: parameterized operator T()


Author: Guy <guy_t1@inter.net.il>
Date: 2000/01/25
Raw View
hi,
    as far as I understand you (and I also tried similar code), the use of
parametrized operator T() means that:

int *ip = new int();
int iv = 3;
int &ir = *ip;

//these are three different types

ip =  Foo(); // means ip is NULL

iv = Foo(); /means iv is zero;

ir = Foo(); //means ir should raise exception - because you cannot set value of
reference as 0 (which is NULL).

    that why I cant understand why would you want to the parametrized operator
T() from the begining(?)...
    it is because I cannot see how the compiler can make correct conversion for
each type.

thanks
        Guy Tal
Thant Tessman wrote:

> Thanks for the responses. I should have mentioned in my original post
> that the program actually does compile on MSVC++6SP3, but that when you
> run it crashes and goes off into assembly-language never-never-land.
> This does work on the SGI compilers. I just wanted to know if it is
> *supposed* to work so I knew whether or not it was foolish to try to
> chase down a compiler for NT that actually handles this.
>
> Frangois Guillemi wrote:
> >
> > VC++ supports member templates and this code compiles althrough I did not
> > check it runs properly. You probably do not have the last service pack.
>
> [...]
>
> > > > Yet another bug in MSVC++ or is this not supposed to work?
> > > >
> > > > struct Foo {
> > > >
> > > >   template <class T>
> > > >   operator T() {
> > > >
> > > >     return 0;
> > > >   }
> > > > };
> > > >
> > > > int main() {
> > > >
> > > >   Foo foo;
> > > >   int i(foo);
> > > >
> > > >   return 0;
> > > > }

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/01/25
Raw View


Guy wrote:
>
>
>
> ir = Foo(); //means ir should raise exception - because you cannot set value of
> reference as 0 (which is NULL).

There's nothing that requires an exception.  As soon as you did *ip you started
invoking undefined behavior and there's no guarantees what will happen.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Thant Tessman <thant@acm.org>
Date: 2000/01/26
Raw View

Guy wrote:

>     that why I cant understand why would you want to the parametrized operator
> T() from the begining(?)...
>     it is because I cannot see how the compiler can make correct conversion for
> each type.

I've written a library that adds "latent typing" to C++. Basically you
can stick any valid C++ value in a variable of type "Value" and get it
back out again. This allows you to, for example, have an array of values
of inhomogeneous types. It uses the trick shown in my original post. For
example:


 struct A {};

 Value v[2];
 v[0] = A(); // an instance of class A
 v[1] = 23;  // an integer

 if (v[0].isa(typeid(A)) {
   A copy_of_a(v[0]);
 }

There's also a (user-extendable) dictionary of converters. For example:

 if (v[1].isa(typeid(double))) {
   double d = v[1];
   /*
      v[1] was really an integer,
      but there's a converter available
   */
 }

If you try to extract a value of the wrong type (or if there is no
converter available), an exception is thrown.

The original motivation for all this was that I was creating a package
for writing out and reading in various kinds of recursive data
structures, and I wanted to be able to extend it without the work of
maintaining scattered case statements.

The "latent typing" library currently compiles with the latest gcc
(which I'm very impressed with), and in theory it should work with the
SGI compilers with little to no modification (since I originally
developed the idea on SGI compilers). If anyone is interested in
receiving the source, e-mail me. If there's enough interest I'll just
post it here (or maybe, more appropriately, comp.lang.c++.moderated).
It's not that big.

I hope eventually that the "latent typing library" will be chapter three
of a book I'd like to write with the tentative title of "Stoopid C++
Tricks: How to Make C++ Suck Less".

-thant

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: 2000/01/26
Raw View
Thant Tessman <thant@acm.org> wrote in message
news:388E25BA.B309C868@acm.org...
> I hope eventually that the "latent typing library" will be chapter three
> of a book I'd like to write with the tentative title of "Stoopid C++
> Tricks: How to Make C++ Suck Less".

There is an article in the latest C++ Report (January 2000) that shows how
to implement a variant type.


Andrei



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: fzhong@my-deja.com
Date: 2000/01/11
Raw View
MSVC++ does not support member template yet.

F. Zhong


In article <3874BCEE.C0AA77CF@acm.org>,
  Thant Tessman <thant@acm.org> wrote:
>
>
> Yet another bug in MSVC++ or is this not supposed to work?
>
> struct Foo {
>
>   template <class T>
>   operator T() {
>
>     return 0;
>   }
> };
>
> int main() {
>
>   Foo foo;
>   int i(foo);
>
>   return 0;
> }
>
> [ comp.std.c++ is moderated.  To submit articles, try just posting
with ]
> [ your news-reader.  If that fails, use mailto:std-
c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting.
---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-
c++/faq.html              ]
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Frangois Guillemi" <francoisg@mainsoft.com>
Date: 2000/01/11
Raw View
VC++ supports member templates and this code compiles althrough I did not
check it runs properly. You probably do not have the last service pack.
Concerning member templates, ATL make some use of it (have a look at class
CComQIPtr) and native COM (see comip.h) relies completely on it.

<fzhong@my-deja.com> wrote in message news:85ejk7$h62$1@nnrp1.deja.com...
> MSVC++ does not support member template yet.
>
> F. Zhong
>
>
> In article <3874BCEE.C0AA77CF@acm.org>,
>   Thant Tessman <thant@acm.org> wrote:
> >
> >
> > Yet another bug in MSVC++ or is this not supposed to work?
> >
> > struct Foo {
> >
> >   template <class T>
> >   operator T() {
> >
> >     return 0;
> >   }
> > };
> >
> > int main() {
> >
> >   Foo foo;
> >   int i(foo);
> >
> >   return 0;
> > }
> >
> > [ comp.std.c++ is moderated.  To submit articles, try just posting
> with ]
> > [ your news-reader.  If that fails, use mailto:std-
> c++@ncar.ucar.edu    ]
> > [              --- Please see the FAQ before posting.
> ---               ]
> > [ FAQ: http://reality.sgi.com/austern_mti/std-
> c++/faq.html              ]
> >
> >
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Thant Tessman <thant@acm.org>
Date: 2000/01/12
Raw View
Thanks for the responses. I should have mentioned in my original post
that the program actually does compile on MSVC++6SP3, but that when you
run it crashes and goes off into assembly-language never-never-land.
This does work on the SGI compilers. I just wanted to know if it is
*supposed* to work so I knew whether or not it was foolish to try to
chase down a compiler for NT that actually handles this.

Frangois Guillemi wrote:
>
> VC++ supports member templates and this code compiles althrough I did not
> check it runs properly. You probably do not have the last service pack.

[...]


> > > Yet another bug in MSVC++ or is this not supposed to work?
> > >
> > > struct Foo {
> > >
> > >   template <class T>
> > >   operator T() {
> > >
> > >     return 0;
> > >   }
> > > };
> > >
> > > int main() {
> > >
> > >   Foo foo;
> > >   int i(foo);
> > >
> > >   return 0;
> > > }
> > >

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Fran ois Guillem " <francoisg@mainsoft.com>
Date: 2000/01/12
Raw View
Indeed, it causes a crash. It seems like the compiler 'forgets'  to
instanciate the member template. More probably, it generates incorrect code
otherwise the linker would catch the missing symbol.
It looks clearly like a MSVC++ bug.

"Thant Tessman" <thant@acm.org> wrote in message
news:387B5758.9FE5DD84@acm.org...
>
> Thanks for the responses. I should have mentioned in my original post
> that the program actually does compile on MSVC++6SP3, but that when you
> run it crashes and goes off into assembly-language never-never-land.
> This does work on the SGI compilers. I just wanted to know if it is
> *supposed* to work so I knew whether or not it was foolish to try to
> chase down a compiler for NT that actually handles this.
>
> Frangois Guillemi wrote:
> >
> > VC++ supports member templates and this code compiles althrough I did
not
> > check it runs properly. You probably do not have the last service pack.
>
> [...]
>
>
> > > > Yet another bug in MSVC++ or is this not supposed to work?
> > > >
> > > > struct Foo {
> > > >
> > > >   template <class T>
> > > >   operator T() {
> > > >
> > > >     return 0;
> > > >   }
> > > > };
> > > >
> > > > int main() {
> > > >
> > > >   Foo foo;
> > > >   int i(foo);
> > > >
> > > >   return 0;
> > > > }

[ moderator's note: excessive quoting removed. Please quote only
  the necessary material. -sdc ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Thant Tessman <thant@acm.org>
Date: 2000/01/06
Raw View

Yet another bug in MSVC++ or is this not supposed to work?


struct Foo {

  template <class T>
  operator T() {

    return 0;
  }
};


int main() {

  Foo foo;
  int i(foo);

  return 0;
}


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]