Topic: explicit


Author: schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann)
Date: 9 Jan 1995 14:28:21 GMT
Raw View
Sorry if this has already been asked:

In which cases can one use the new "explicit" keyword?

1) For all ctors with 1 parameter (Ok, this was the reason to introduce it).
2) For all ctors with n (>1) parameters that have default-arguments
   for all or for all but the first argument ?
3) For any ctor, though it only has an effect for 1) and 2) ?

The following are extension-ideas:
4) For all userdefined cast-operators ? - coz a cast works like a
   1-param-ctor, it's only defined in the other direction.
   con: - Only define cast-operators when you want an implicit call.
          Else name them toInt(), toFoo() etc.
   pro: - Calling them by (int) is the canonical way. I don't want to
          keep in mind all these naming conventions (mine + others + libs):
          _int(), toint(), to_int(), toInt(), ToInt(), mkInt(), make_int() ...

5) Could one use explicit for the destructor, so that one normally can't
   work with instances of storage-class auto?

6) Could one use explicit for the default-ctor, so that one HAS TO write
   "Foo foo = Foo();" instead of "Foo foo;" ?
   [If we had it the other way round: default is explicit, use "implicit"
    for the current behavior, then an implicit default-ctor could
    construct instances out of nothing:
 void f(Foo f);
 f();  // calls f(Foo())
    - A rather dangerous thing.
   ]


Ulf Schuenemann

--------------------------------------------------------------------
Ulf Sch   nemann
Institut f   r Informatik, Technische Universit   t M   nchen.
email: schuenem@informatik.tu-muenchen.de




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 9 Jan 1995 17:24:20 GMT
Raw View
In article bld@hpsystem1.informatik.tu-muenchen.de, schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann) writes:
>
>Sorry if this has already been asked:
>
>In which cases can one use the new "explicit" keyword?
>
>1) For all ctors with 1 parameter (Ok, this was the reason to introduce it).
>2) For all ctors with n (>1) parameters that have default-arguments
>   for all or for all but the first argument ?
>3) For any ctor, though it only has an effect for 1) and 2) ?

Yes to 1 and 2. I don't think the proposal addressed 3, but if legal it would
have no effect.

>The following are extension-ideas: ...

The "explicit" keyword was introduced to solve the problem of unintended use
of one-argument constructors being used implicitly as type conversions when
not intended by the programmer. It has no other purpose. Some extensions of
the idea were considered and rejected.

---
Steve Clamage, stephen.clamage@eng.sun.com