Topic: why can't operators be declared explicit.
Author: brok@rubikon.pl (Bronek Kozicki)
Date: Sun, 29 Aug 2004 00:20:44 GMT Raw View
Conrad Weyns wrote:
> There is no way I can remove these operators nor would I want to. But it
> would sure help if I could declare them explicit.
> Evidently, that is not legal. Why is the explicit keyword reserved to ctors?
C++ Standard Committee is considering such modification of the language,
that it will be legal. For details see
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1592.pdf
B.
---
[ 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: v.Abazarov@comAcast.net ("Victor Bazarov")
Date: Sun, 29 Aug 2004 00:21:24 GMT Raw View
"Conrad Weyns" <weyns@online.no> wrote...
> This is probably a stupid question and I am going to fall flat on my
face -
> again :-)
> I am in the process of "expliciting" one parameter ctors in some legacy
> code. Typically:
> class String
> {
> public:
> explicit String(int);
> explicit String(double);
> .....
> };
>
> So far so good. But, the code also has loads of implicit operators like:
>
> class String
> {
> public:
> .....
> operator int() const;
> operator double() const;
> .....
> };
>
> There is no way I can remove these operators nor would I want to. But it
> would sure help if I could declare them explicit.
> Evidently, that is not legal. Why is the explicit keyword reserved to
ctors?
I read something RE making other things explicit, but I can't remember what
exactly and where. But here is my take on the problem: constructors create
objects. The ability of them to provide [implicit] conversions is an added
benefit, which sometimes can hurt, and that's why it can be removed by use
of 'explicit' declaration. The type conversion operators are there _only_
to provide [implicit] conversions, so, why would you want to remove that
capability by using 'explicit' there? Wouldn't that make type conversion
operators essentially useless? I know, I know, you could still write
int a = int(myString);
and [supposedly] that would call the _explicit_ type conversion operator,
but why? Wouldn't you want to simply write
int a = myString;
? And if you don't provide 'operator double', this
double a = myString;
would work just as well. Think about it. If you later provide 'operator
double' and recompile, you don't need to change the code. If your type
conversion operators are explicit, and you had
double a = int(myString);
then you need to look for things like that and potentially change them to
double a = double(myString);
...
Victor
---
[ 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: mvorbrodt@poczta.onet.pl ("Martin Vorbrodt")
Date: Sun, 29 Aug 2004 00:24:03 GMT Raw View
because that's what they are defined to be by the standard, sorry:)
remember, only one implicit type conversion is applied per object, and it is
applied only if it is not ambiguous.
Look ANSI/ISO C++ Standard, page 188, section 12.3 Conversions.
""Conrad Weyns"" <weyns@online.no> wrote in message
news:hiAXc.3554$WW4.49142@news4.e.nsc.no...
> This is probably a stupid question and I am going to fall flat on my
face -
> again :-)
> I am in the process of "expliciting" one parameter ctors in some legacy
> code. Typically:
> class String
> {
> public:
> explicit String(int);
> explicit String(double);
> .....
> };
>
> So far so good. But, the code also has loads of implicit operators like:
>
> class String
> {
> public:
> .....
> operator int() const;
> operator double() const;
> .....
> };
>
> There is no way I can remove these operators nor would I want to. But it
> would sure help if I could declare them explicit.
> Evidently, that is not legal. Why is the explicit keyword reserved to
ctors?
>
> regards,
> Conrad Weyns
>
> ---
> [ 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 ]
>
---
[ 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: brangdon@cix.co.uk (Dave Harris)
Date: Sun, 29 Aug 2004 16:24:41 GMT Raw View
weyns@online.no ("Conrad Weyns") wrote (abridged):
> Why is the explicit keyword reserved to ctors?
If you want explicit conversion, you can do so via a normal function:
class String {
// ...
};
template<typename T> T to( const String & );
template<> int to( const String & );
template<> double to( const String & );
void test() {
String string;
int i = to<int>(string);
double d = to<double>(string);
}
-- Dave Harris, Nottingham, UK
---
[ 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: weyns@online.no ("Conrad Weyns")
Date: Sat, 28 Aug 2004 00:25:11 GMT Raw View
This is probably a stupid question and I am going to fall flat on my face -
again :-)
I am in the process of "expliciting" one parameter ctors in some legacy
code. Typically:
class String
{
public:
explicit String(int);
explicit String(double);
.....
};
So far so good. But, the code also has loads of implicit operators like:
class String
{
public:
.....
operator int() const;
operator double() const;
.....
};
There is no way I can remove these operators nor would I want to. But it
would sure help if I could declare them explicit.
Evidently, that is not legal. Why is the explicit keyword reserved to ctors?
regards,
Conrad Weyns
---
[ 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 ]