🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3395

Re: Explicit? What's that?

#3395
From: Steve Clamage
Date: Wed, 21 Feb 1996 23:41
44 lines
2662 bytes
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 
]
Technical Details
Message-ID: <4gg8p5$lcq@engnews1.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
References: <4gg7df$76c@charnel.ecst.csuchico.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
From: clamage@Eng.Sun.COM (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Explicit?  What's that?
Date: 21 Feb 1996 15:41:53 PST
Organization: Sun Microsystems Inc.
Approved: austern@isolde.mti.sgi.com
Message-ID: <4gg8p5$lcq@engnews1.Eng.Sun.COM>
References: <4gg7df$76c@charnel.ecst.csuchico.edu>
Reply-To: clamage@Eng.Sun.COM
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: 21 Feb 1996 23:10:29 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMSut2Uy4NqrwXLNJAQEcAAH8DUY2eWb8enoURRytfzeZ9lLl0qUs3bIe
 jK9N3lqimJvXNXHMIFnK1TFFluaaG+A4UY6fMJPyaGzjB2ShMlgwoQ==
 =A8yL
Originator: austern@isolde.mti.sgi.com