Topic: Explicit conversion operators


Author: kprateek88@yahoo.com (Prateek R Karandikar)
Date: Sun, 18 Apr 2004 21:09:44 +0000 (UTC)
Raw View
kuyper@wizard.net (James Kuyper) wrote in message news:<8b42afac.0404140852.138566cd@posting.google.com>...
> qrczak@knm.org.pl ("Marcin 'Qrczak' Kowalczyk") wrote in message news:<pan.2004.04.13.16.12.50.190170@knm.org.pl>...
> > On Tue, 13 Apr 2004 15:02:56 +0000, James Kuyper wrote:
> >
> > > The reason why they're not allowed is that you can achieve the same
> > > effect with a user-named function, such as A::makeB().
> >
> > The effect is not the same. If the type name is embedded in the method
> > name, it can't come from a template parameter.
>
> True, but it's trivial to deal with if you need to do that. Defining
> A::make<B>() is perfectly feasible.
>
> ---
> [ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]


However, this is not a universal syntax for explicit conversion. One
class might  declare A::make<B>(). Another might declare
C::convert<D>. In a template class, we would not be able to use a
uniform syntax. Had explicit conversion operators been allowed, we
could have used a single, universal syntax : static_cast<T>(x).

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dtmoore@rijnh.nl (Dave Moore)
Date: Mon, 19 Apr 2004 17:35:58 +0000 (UTC)
Raw View
kprateek88@yahoo.com (Prateek R Karandikar) wrote in message news:<607f883e.0404180152.130a42aa@posting.google.com>...
> kuyper@wizard.net (James Kuyper) wrote in message news:<8b42afac.0404140852.138566cd@posting.google.com>...
> > qrczak@knm.org.pl ("Marcin 'Qrczak' Kowalczyk") wrote in message news:<pan.2004.04.13.16.12.50.190170@knm.org.pl>...
> > > On Tue, 13 Apr 2004 15:02:56 +0000, James Kuyper wrote:
> > >
> > > > The reason why they're not allowed is that you can achieve the same
> > > > effect with a user-named function, such as A::makeB().
> > >
> > > The effect is not the same. If the type name is embedded in the method
> > > name, it can't come from a template parameter.
> >
> > True, but it's trivial to deal with if you need to do that. Defining
> > A::make<B>() is perfectly feasible.
> >
> > ---
> > [ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]
>
>
> However, this is not a universal syntax for explicit conversion. One
> class might  declare A::make<B>(). Another might declare
> C::convert<D>. In a template class, we would not be able to use a
> uniform syntax. Had explicit conversion operators been allowed, we
> could have used a single, universal syntax : static_cast<T>(x).
>

Hmmm .. without spending too much time thinking about this, I am
finding hard to come up with an example where the template writer
would not already be aware of or be within his rights to enforce a
particular syntax for the named conversion operator (i.e. whether to
use B::make<A> or B::convert<A>).  Perhaps I am being dense, but a
counter-example might help focus the discussion here.

  As I see it, we aren't dealing with the "is-a" formalism of class
hierarchies here, but rather something like a "is-part-of" or
"can-be-extracted-from" situation, for which it seems debatable
whether or not type-conversion is really appropriate.  Reading between
the lines from the original post, it seems like what the OP wants is
something that fulfills:  "don't always allow conversion of B to the
contained type A, but just do it when I ask you to specifically."  So
it seems that a named conversion function is most appropriate, as
already suggested in this thread.

   So, I guess I agree with the poster from the older version of this
thread, who equated explicit conversion operators with syntactic sugar
.. and as such something that does not really mandate a modification
to the standard. But of course this might just be because I haven't
encountered an example where an explicit type-conversion is really
needed.

  As an afterthought, I am starting to get a fuzzy idea that explicit
type conversion *might* be of use in template meta-programming and/or
for compile-time assertion generation, but I am just learning about
those myself, so I could be wrong.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Nicola.Musatti@ObjectWay.it (Nicola Musatti)
Date: Tue, 20 Apr 2004 20:50:48 +0000 (UTC)
Raw View
dtmoore@rijnh.nl (Dave Moore) wrote in message news:<306d400f.0404190659.ada2804@posting.google.com>...
[...]
> Hmmm .. without spending too much time thinking about this, I am
> finding hard to come up with an example where the template writer
> would not already be aware of or be within his rights to enforce a
> particular syntax for the named conversion operator (i.e. whether to
> use B::make<A> or B::convert<A>).  Perhaps I am being dense, but a
> counter-example might help focus the discussion here.

One example could be the introduction into an existing project of a
third party library that uses a different convention.

[...]
>    So, I guess I agree with the poster from the older version of this
> thread, who equated explicit conversion operators with syntactic sugar
> .. and as such something that does not really mandate a modification
> to the standard. But of course this might just be because I haven't
> encountered an example where an explicit type-conversion is really
> needed.

Personally I believe it would be better if *all* conversions where
required to be explicit, especially those that do not guarantee the
preservation of the original value. However I'm aware that the lack of
templates made  this impractical, and that it is too late to change
now.

Cheers,
Nicola Musatti

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net (James Kuyper)
Date: Wed, 14 Apr 2004 17:44:35 +0000 (UTC)
Raw View
qrczak@knm.org.pl ("Marcin 'Qrczak' Kowalczyk") wrote in message news:<pan.2004.04.13.16.12.50.190170@knm.org.pl>...
> On Tue, 13 Apr 2004 15:02:56 +0000, James Kuyper wrote:
>
> > The reason why they're not allowed is that you can achieve the same
> > effect with a user-named function, such as A::makeB().
>
> The effect is not the same. If the type name is embedded in the method
> name, it can't come from a template parameter.

True, but it's trivial to deal with if you need to do that. Defining
A::make<B>() is perfectly feasible.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rlk@isid.ac.in (Rajeeva Karandikar)
Date: Mon, 12 Apr 2004 18:44:37 +0000 (UTC)
Raw View
This is a multi-part message in MIME format.
--------------090602010701090804010005
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Why isn't it possible to declare explicit conversion operators? Suppose
we are developing a class B, using a class A. b is of type B. We don't
want b to be acceptable where an A is required, but we want A(b) to be
acceptable. So we can't use B::operator A(), because that is an implicit
conversion operator. If B::operator A() has an effect similar to that of
A::A(const B&) as far as conversions are concerned, then the explicit
B::operator A() should have an effect similar to the explicit A::A(const
B&), as far as conversions are concerned.

--
Prateek Karandikar
7, S J S Sansanwal Marg, New Delhi 110016 INDIA

Phone 91-11-26965467(home):    email : rlk@isid.ac.in


--------------090602010701090804010005
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
  <title></title>
</head>
<body text="#000099" bgcolor="#ffccff">
<big><big>Why isn't it possible to declare explicit conversion
operators? Suppose we are developing a class B, using a class A. b is
of type B. We don't want b to be acceptable where an A is required, but
we want A(b) to be acceptable. So we can't use B::operator A(), because
that is an implicit conversion operator. If B::operator A() has an
effect similar to that of A::A(const B&amp;) as far as conversions are
concerned, then the explicit B::operator A() should have an effect
similar to the explicit A::A(const B&amp;), as far as conversions are
concerned.<br>
</big></big><br>
<pre class="moz-signature" cols="72">--
Prateek Karandikar
7, S J S Sansanwal Marg, New Delhi 110016 INDIA

Phone 91-11-26965467(home):    email : <a class="moz-txt-link-abbreviated" href="mailto:rlk@isid.ac.in">rlk@isid.ac.in</a>
</pre>
</body>
</html>

--------------090602010701090804010005--

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net (James Kuyper)
Date: Tue, 13 Apr 2004 15:02:56 +0000 (UTC)
Raw View
rlk@isid.ac.in (Rajeeva Karandikar) wrote in message news:<4078E458.1060007@isid.ac.in>...
> Why isn't it possible to declare explicit conversion operators? Suppose
> we are developing a class B, using a class A. b is of type B. We don't
> want b to be acceptable where an A is required, but we want A(b) to be
> acceptable. So we can't use B::operator A(), because that is an implicit
> conversion operator. If B::operator A() has an effect similar to that of
> A::A(const B&) as far as conversions are concerned, then the explicit
> B::operator A() should have an effect similar to the explicit A::A(const
> B&), as far as conversions are concerned.

The reason why they're not allowed is that you can achieve the same
effect with a user-named function, such as A::makeB(). The reason why
'explicit' is needed for constructors is that constructors are so
special in their nature that a user-named function can't substitute
for a constructor.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: qrczak@knm.org.pl ("Marcin 'Qrczak' Kowalczyk")
Date: Tue, 13 Apr 2004 19:34:12 +0000 (UTC)
Raw View
On Tue, 13 Apr 2004 15:02:56 +0000, James Kuyper wrote:

> The reason why they're not allowed is that you can achieve the same
> effect with a user-named function, such as A::makeB().

The effect is not the same. If the type name is embedded in the method
name, it can't come from a template parameter.

--
   __("<         Marcin Kowalczyk
   \__/       qrczak@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]