Topic: Rationale for 7.1.2/6


Author: ark@research.att.com (Andrew Koenig)
Date: Tue, 26 Nov 2002 18:05:23 +0000 (UTC)
Raw View
Carl> 7.1.2/6 says "The explicit specifier shall be used only in
Carl> declarations of constructors within a class declaration; see
Carl> 12.3.1."

Carl> Why?  In particular, it seems to me that it'd be useful to be
Carl> able to apply 'explicit' to conversion operators (e.g. operator
Carl> int()).

Carl> Is there some reason that explicit was so severely limited by
Carl> definition?

Yes--the reason is that we couldn't figure out how explicit conversion
operators should interact with inheritance.  Constructors aren't
inherited, so they don't have that problem.

--
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark

---
[ 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: Andy Sawyer <news@evo6.com>
Date: Tue, 26 Nov 2002 12:05:01 CST
Raw View
In article <2ONE9.4412$sW.228339932@newssvr13.news.prodigy.com>,
 on Tue, 26 Nov 2002 17:11:55 +0000 (UTC),
 cpdaniel@mvps.org ("Carl Daniel") wrote:

> 7.1.2/6 says
>
> "The explicit specifier shall be used only in declarations of constructors
> within a class declaration; see
> 12.3.1."
>
> Why?  In particular, it seems to me that it'd be useful to be able to apply
> 'explicit' to conversion operators (e.g. operator int()).

I'd have said that if you don't want implicit conversions to take
place, you probably shouldn't write conversion operators. If you want
explicit conversion operators, give them names like (e.g.) "as_int()"
and call them explicitly.

> Is there some reason that explicit was so severely limited by definition?

I don't think you can reasonably claim that it's "severely limited" -
it does exactly what it was intended to do. Of course, if your
argument is persuasive enough, you may be able to encourage the
commitee to embrace your suggestion in the next standard...

Regards,
 Andy S.
--
"Light thinks it travels faster than anything but it is wrong. No matter
 how fast light travels it finds the darkness has always got there first,
 and is waiting for it."                  -- Terry Pratchett, Reaper Man

---
[ 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: anthony.williamsNOSPAM@anthonyw.cjb.net (Anthony Williams)
Date: Wed, 27 Nov 2002 16:48:30 +0000 (UTC)
Raw View
Andy Sawyer <news@evo6.com> writes:

> In article <2ONE9.4412$sW.228339932@newssvr13.news.prodigy.com>,
>  on Tue, 26 Nov 2002 17:11:55 +0000 (UTC),
>  cpdaniel@mvps.org ("Carl Daniel") wrote:
>
> > 7.1.2/6 says
> >
> > "The explicit specifier shall be used only in declarations of constructors
> > within a class declaration; see
> > 12.3.1."
> >
> > Why?  In particular, it seems to me that it'd be useful to be able to apply
> > 'explicit' to conversion operators (e.g. operator int()).
>
> I'd have said that if you don't want implicit conversions to take
> place, you probably shouldn't write conversion operators. If you want
> explicit conversion operators, give them names like (e.g.) "as_int()"
> and call them explicitly.
>
> > Is there some reason that explicit was so severely limited by definition?
>
> I don't think you can reasonably claim that it's "severely limited" -
> it does exactly what it was intended to do. Of course, if your
> argument is persuasive enough, you may be able to encourage the
> commitee to embrace your suggestion in the next standard...

There was a long discussion either here or on comp.lang.c++.moderated about
this, recently. Basically, it comes down to this: if you're writing generic
code, how do you know how to spell "as_int()" without requiring the user of
the class to specialize a traits template? Bear in mind that this traits
template will end up being called different things for every different library
that uses it, and also different libraries will require "as_double()",
"as_string()" and "as_foo()".

Anthony
--
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.

---
[ 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: cpdaniel@mvps.org ("Carl Daniel")
Date: Tue, 26 Nov 2002 17:11:55 +0000 (UTC)
Raw View
7.1.2/6 says

"The explicit specifier shall be used only in declarations of constructors
within a class declaration; see
12.3.1."

Why?  In particular, it seems to me that it'd be useful to be able to apply
'explicit' to conversion operators (e.g. operator int()).

Is there some reason that explicit was so severely limited by definition?

-cd

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