Article View: comp.std.c++
Article #4052Re: C++ syntactic trap
From: John R. Williams
Date: Mon, 15 Apr 1996 22:32
Date: Mon, 15 Apr 1996 22:32
55 lines
3122 bytes
3122 bytes
J. Kanze (kanze@gabi-soft.fr) wrote:
> In article <4kcrhk$aj1@hermes.acs.unt.edu> johnw@jove.acs.unt.edu (John
> R. Williams) writes:
> [Concerning a warning for `new char( 10 )'...]
> |> I agree that it is uusual to allocate a single object of a built-in type
> |> this way, but how do you propose to write this in such a way that such a
> |> warning would not be generated? All I can think of is to warn when *any*
> |> conversion occurs (this one looks pretty explicit to me), forcing you to
> |> write something like this:
> |> char *foo = new char(static_cast<char>(10));
> |> (Actually this rule doesn't seem too bad if applied only to built-in
> |> types...)
> Let's see:
> int* pi = new int( static_cast< int >( 10 ) ) ;
> Do you want the compiler to warn if you omit the static_cast (of int to
> int)?
What I meant (but perhaps did not make clear) is that the warning would
only be in the case of *conversions* (and possibly promotions). In this
case my rule would allow more comfortable syntax without a warning.
> I do not think that I have a single instance of allocation of an array
> in any of my code. Why should you make the most frequent case the most
> difficult. (And allocating a single int is not that infrequent. You
> might look at the implementation of counted_ptr in Barton and Nackman,
> for example. It is certainly more frequent than allocating an array of
> int's, at least in well written C++ code.)
I admit that since I get a copy of HP's STL I have almost entirely
stopped using array new, but OTOH I can't remeber a single time I've
allocated just one object of a built-in type. This suggests to me that
the issue is one of of style and context, not ability to program.
At any rate, I don't like my idea either--I was just throwing it out to
see what people would think.
--
class JohnWilliams: public Student, public Programmer {
public:
char const *operator&() const { return "johnw@jove.acs.unt.edu"; }
char const *homepage () const { return "http://www.unt.edu/~johnw"; }
};
[ 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 ]
[ 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:
<4kuigg$nbr@hermes.acs.unt.edu>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageReferences:
<4k3q4p$lkd@syn.cs.cornell.edu> <4kcrhk$aj1@hermes.acs.unt.edu> <KANZE.96Apr10111924@gabi.gabi-soft.fr>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: johnw@jove.acs.unt.edu (John R. Williams) Newsgroups: comp.std.c++ Subject: Re: C++ syntactic trap Date: 15 Apr 1996 22:32:24 GMT Organization: University of North Texas Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <4kuigg$nbr@hermes.acs.unt.edu> References: <4k3q4p$lkd@syn.cs.cornell.edu> <4kcrhk$aj1@hermes.acs.unt.edu> <KANZE.96Apr10111924@gabi.gabi-soft.fr> NNTP-Posting-Host: taumet.eng.sun.com X-Nntp-Posting-Host: jove.acs.unt.edu X-Newsreader: TIN [version 1.2 PL2] Content-Length: 2058 X-Lines: 48 Originator: clamage@taumet