Topic: Explicit? What's that?


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/02/21
Raw View
In article 76c@charnel.ecst.csuchico.edu, mcelroy@ecst.CSUChico.EDU (James Robert McElroy) writes:
>Hmmm...  About two years ago I suggested, in this
>newsgroup, that the "explicit" keyword be used
>by C++.  But my suggestion was for use with
>conversion operators rather than constructors.
>If I remember correctly, the majority of people
>thought it  was a stupid idea.  Interesting...

Whether "explicit" should also apply to type conversion operators
has been discussed here and in the C++ committee. Some people on
the committee thought that the proposal to add "explicit" for
constructors also applied to conversion operators, but the
proposal did not contain that wording.

Personally, I don't see much utility in "explicit" for conversion
operators. If you don't want a type conversion to be implicit, don't
use a conversion operator, use a named function instead. Example:
 class A {
  ...
  int to_int(); // instead of "operator int()"
  friend int to_int(const A&); // another alternative
 };
 A a;
 int i = a; // error
 int j = a.to_int(); // instead of "static_cast<int>(a)"
 int k = to_int(a); // another alternative

You don't have this option with constructors, and the workarounds for
the lack of "explicit" constructors are often ugly.

The only advantage I can see for an "explicit" conversion operator is
that a name for a complicated type, especially one involving pointers
or references, might be inconvenient and not intuitive. In that
case you might prefer to be able to cast to char**& instead of thinking
up a name for that type.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ To submit articles: Try just posting with your newsreader.  If that fails,
                      use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: hxh@jhk.com
Date: 1996/02/22
Raw View
In <4gb00j$2ne@charnel.ecst.csuchico.edu>, mcelroy@ecst.CSUChico.EDU
(James Robert McElroy) writes:
|>Borland sent me an upgrade offer for C++ V. 5.0.  Among
|>the new features they say they support is "Explicit".
|>No mention of what "Explicit" means is given.
|>
|>What is this critter?

It is a new feature in evolving C++ language.  It servers as a
qualifier in front of the constructor to instruct the compiler not to
do implicit type conversion which may cause some subtle bugs in code,
especially in single argument constructor where a proxy class
constructor is usually used to avoid such conversion.
---
[ To submit articles: Try just posting with your newsreader.  If that fails,
                      use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/02/22
Raw View
clamage@Eng.Sun.COM (Steve Clamage) writes:

>The only advantage I can see for an "explicit" conversion operator is
>that a name for a complicated type, especially one involving pointers
>or references, might be inconvenient and not intuitive. In that
>case you might prefer to be able to cast to char**& instead of thinking
>up a name for that type.

`explicit' conversion operations might be useful in templates.
They would make it easy for a template to work both with data types
that have an explicit conversion operator and also with data types
which just use an implicit conversion operator.

--
Fergus Henderson              WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au               PGP: finger fjh@128.250.37.3
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]





Author: mcelroy@ecst.CSUChico.EDU (James Robert McElroy)
Date: 1996/02/23
Raw View
I agree with Pablo -- it would be nice to be able
to use the explicit keyword on conversion  operators
too.  Is it too late to influence anyone on this?
Hey Bjarne!  Are you out there?
--
Jim McElroy
Calif. State Univ., Chico
mcelroy@ecst.csuchico.edu
[ To submit articles: Try just posting with your newsreader.
        If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: mcelroy@ecst.CSUChico.EDU (James Robert McElroy)
Date: 1996/02/20
Raw View
Borland sent me an upgrade offer for C++ V. 5.0.  Among
the new features they say they support is "Explicit".
No mention of what "Explicit" means is given.

What is this critter?
--
Jim McElroy
Calif. State Univ., Chico
mcelroy@ecst.csuchico.edu
---
[ To submit articles: try just posting with your news-reader.
                      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu.
]