Topic: Conversion operator declaration/use inconsequence


Author: nikb@webmaster.com ("Nikolaos D. Bougalis")
Date: Thu, 8 Jul 2004 22:51:38 +0000 (UTC)
Raw View
Robert Kawulak wrote:

>>Your original assertion was that the conversion operator is invoked
>>through static_cast, so should be defined using the keyword static_cast.
>
>
>     Sorry, I didn't express myself clearly enough. I'll try to explain my
> reasoning. Now, calling static_cast<T>(), implicit casting or C-style
> casting means calling operator T () (at least that's how I see it). I
> understand that this is caused by late introduction of static_cast. What I
> mean is that this could be changed to: implicit casting or C-style casting
> means calling static_cast<T>(), which would be defined in classes in
> replacement of operator T (). It just seems to me more natural as
> static_cast is prefered to C-style cast. So:

 Just because it seems more natural to you does not make it reasonable.
Besides breaking all existing code that uses operator overloads it would
end up in the following scenario:

 Someone would post, saying "It seems unnatural to me to define
static_cast<int> if I want to type (int)mytype." Or "I want two operator
overloads: one for static_cast<int> and one for (int) so I can
distinguish which was called."


>     I realise this is almost solely an aesthetic innovation and it would
> affect existing code. The purpose of my post was only to point out this
> inconsequence (as I consider it to be), it's not up to me to decide ;-)

 What "inconsequence"? You define "operators" for your class that can be
called any number of ways to perform conversions. You just don't like
the way it looks.

 -n

---
[ 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: tigrisek@interia.pl ("Robert Kawulak")
Date: Sat, 3 Jul 2004 01:21:20 +0000 (UTC)
Raw View
Conversion operators are being declared in a way that differs very much from
the way they're called:

    struct c {
      operator int (); //declaration of conversion operator
    }; //struct c

    static_cast<int>(c()); //use of conversion operator

Should't such operators be declared and used in an uniform way? The
declaration could look like this:

    int static_cast(); //declaration of conversion operator

or:

    static_cast<int>(); //declaration of conversion operator

instead of

    operator int (); //declaration of conversion operator

Another serious inconsequence is letting conversion operators implemented as
constructors taking one argument be explicit, while not doing so with
ordinary conversion operators (see paper n1592).

Greetings,
RK


---
[ 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: petebecker@acm.org (Pete Becker)
Date: Sat, 3 Jul 2004 03:06:10 +0000 (UTC)
Raw View
Robert Kawulak wrote:
>
> Conversion operators are being declared in a way that differs very much from
> the way they're called:
>
>     struct c {
>       operator int (); //declaration of conversion operator
>     }; //struct c
>
>     static_cast<int>(c()); //use of conversion operator
>

void f(int);
c obj;

f(obj);   // no static_cast
double d = (int)obj; // no static_cast
int i = obj;  // no static_cast

static_cast isn't the only way to use a conversion operator.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ 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: tigrisek@interia.pl ("Robert Kawulak")
Date: Sat, 3 Jul 2004 19:16:52 +0000 (UTC)
Raw View
> > Conversion operators are being declared in a way that differs very much
from
> > the way they're called:
> >
> >     struct c {
> >       operator int (); //declaration of conversion operator
> >     }; //struct c
> >
> >     static_cast<int>(c()); //use of conversion operator
> >
>
> void f(int);
> c obj;
>

> f(obj); // no static_cast

There's no difference here, because conversion is implicit.

> double d = (int)obj; // no static_cast

This is C-style cast and it's deprecated (am I right?)

> int i = obj; // no static_cast

Same situation as f(obj).

Greetings,
RK


---
[ 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: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Sat, 3 Jul 2004 22:04:11 +0000 (UTC)
Raw View
In article <cc26o3$bl8$1@nemesis.news.tpi.pl>, Robert Kawulak
<tigrisek@interia.pl> writes
>Conversion operators are being declared in a way that differs very much from
>the way they're called:

Your post predicates something that is not the case. To understand the
syntax for declaring a conversion operator note:

1) conversion operators and therefore the syntax for declaring them long
anti-dates the introduction of the new style casts.

2) conversion operators are also called implicitly as well as being
callable by an explicit C-style cast.


--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

---
[ 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: petebecker@acm.org (Pete Becker)
Date: Sun, 4 Jul 2004 07:22:06 +0000 (UTC)
Raw View
Robert Kawulak wrote:
>
> > > Conversion operators are being declared in a way that differs very much
> from
> > > the way they're called:
> > >
> > >     struct c {
> > >       operator int (); //declaration of conversion operator
> > >     }; //struct c
> > >
> > >     static_cast<int>(c()); //use of conversion operator
> > >
> >
> > void f(int);
> > c obj;
> >
>
> > f(obj); // no static_cast
>
> There's no difference here, because conversion is implicit.

No difference from what?

>
> > double d = (int)obj; // no static_cast
>
> This is C-style cast and it's deprecated (am I right?)

Yes, it's a C-style cast. Yes, it's deprecated. Yes, it's part of the
C++ language. Yes, it's commonly used.

>
> > int i = obj; // no static_cast
>
> Same situation as f(obj).
>

Whatever that means.

Your original assertion was that the conversion operator is invoked
through static_cast, so should be defined using the keyword static_cast.
I showed you three ways of invoking the conversion operator that do not
use static_cast, which further weakens your already weak argument.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ 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: tigrisek@interia.pl ("Robert Kawulak")
Date: Mon, 5 Jul 2004 16:16:32 +0000 (UTC)
Raw View
> Your original assertion was that the conversion operator is invoked
> through static_cast, so should be defined using the keyword static_cast.

    Sorry, I didn't express myself clearly enough. I'll try to explain my
reasoning. Now, calling static_cast<T>(), implicit casting or C-style
casting means calling operator T () (at least that's how I see it). I
understand that this is caused by late introduction of static_cast. What I
mean is that this could be changed to: implicit casting or C-style casting
means calling static_cast<T>(), which would be defined in classes in
replacement of operator T (). It just seems to me more natural as
static_cast is prefered to C-style cast. So:

> I showed you three ways of invoking the conversion operator that do not
> use static_cast, which further weakens your already weak argument.

    They do not use static_cast explicitly, but they consist in calling
casting function. Now, casting function is defined as operator T (). It
might be defined as static_cast<T>(). Therefore invoking conversion operator
would mean invoking static_cast.

> > > f(obj); // no static_cast
> >
> > There's no difference here, because conversion is implicit.
>
> No difference from what?

    There's no difference whether you define static_cast<T>() or operator T
(), because the one you provide is considered to be the casting function.
There could be only one of them defined, and the first style would be
prefered while the other would be deprecated.
    I realise this is almost solely an aesthetic innovation and it would
affect existing code. The purpose of my post was only to point out this
inconsequence (as I consider it to be), it's not up to me to decide ;-)

Greetings,
RK


---
[ 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                       ]