Topic: Global conversion functions (operator cast)


Author: google@peterritchie.com (Peter Ritchie)
Date: Sat, 1 Feb 2003 00:32:42 +0000 (UTC)
Raw View
Hello,

I've had a peek at the archive and the FAQ and haven't noticed
anything involving *why* conversion operators must be members.

12.3.2 only mentions member conversion functions and does not mention
global conversion functions or that global conversion functions are
illegal.

Given that I can write a function:

convert(const mytype1 & from, mytype2 & to)
{
// do something...
}

Why would a global conversion function not be legal:
operator mytype1(mytype2 rhs)
{
 mytype1 lhs;
 // do something ...
 return lhs;
}

Of course, it's likely that performance issues would arise from all
the object duplication.  Hence an alternative syntax (dissimilar to
existing conversion operator syntax):

operator mytype1(mytype1 & lhs, const mytype2 & rhs)
{
// do something ...
}

This would allow user-defined types to operate with the same syntax as
built-in types without having to use different syntax through a named
conversion function (e.g. ConvertAtoB()).

-- Peter

---
[ 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: stephen.clamage@sun.com (Stephen Clamage)
Date: Mon, 3 Feb 2003 19:56:03 +0000 (UTC)
Raw View
On Sat, 1 Feb 2003 00:32:42 +0000 (UTC), google@peterritchie.com
(Peter Ritchie) wrote:

>Hello,
>
>I've had a peek at the archive and the FAQ and haven't noticed
>anything involving *why* conversion operators must be members.
>
>12.3.2 only mentions member conversion functions and does not mention
>global conversion functions or that global conversion functions are
>illegal.
>
>Given that I can write a function:
>
>convert(const mytype1 & from, mytype2 & to)
>{
>// do something...
>}
>
>Why would a global conversion function not be legal:
>operator mytype1(mytype2 rhs)
>{
> mytype1 lhs;
> // do something ...
> return lhs;
>}
>
>Of course, it's likely that performance issues would arise from all
>the object duplication.  Hence an alternative syntax (dissimilar to
>existing conversion operator syntax):
>
>operator mytype1(mytype1 & lhs, const mytype2 & rhs)
>{
>// do something ...
>}
>
>This would allow user-defined types to operate with the same syntax as
>built-in types without having to use different syntax through a named
>conversion function (e.g. ConvertAtoB()).

You cannot write an implicit conversion between built-in types. Such
conversions are predefined (or illegal) in the language.

A conversion function from a built-in type to a user-defined type is
not distinguishable from a constructor, and so must be a constructor.

The only other conversion is from a user-defined type to some other
type. What problem is solved by allowing the conversion to be other
than a member of that class?

And I don't understand your last paragraph. You don't need to use
special syntax to invoke conversion functions:

class C {
public:
 operator int();
};
C c;
int i = c; // implicitly convert from C to int
int j = (int)c;
int k = static_cast<int>(c);

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