|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-10-25 08:01:55
From: "Kevin Lynch" <krlynch_at_[hidden]>
> > > namespace math
> > > {
> > > float const pi_f = ...;
> > > double const pi = ...;
> > > long double const pi_l = ...;
> > > }
>
> Well, I hate to be difficult, but I'm going to be anyway. While I
> understand the arguments people have made, I don't agree with any of
> them :-) Templates solve precisely this "multiple names for the same
> concept but with different types" problem. I, personally, would not use
> the library in this form, as I find this to be an inelegant and fairly
> dangerous hack for a number of reasons:
>
> It makes code maintenance a nightmare, since code becomes brittle under
> type changes, or even under typos! How many times have you mistakenly
> written "doubel"? The compiler can catch that problem, but it is not an
> error to write pi_f when you mean pi_l, and I don't believe a compiler
> is required to tell you about the mistake (of course, I might be
> mistaken about that, and the implementation I use does warn you that you
> are making a mistake). The suffixes are ugly warts carrying type
> information, and I don't see that it really improves ease of use all
> that much. The language has mechanisms to eliminate these problems, and
> the compiler is there to enforce correct use of those mechanisms to make
> coding easier and more expressive. I haven't heard reasons that are
> compelling enough for me to want to throw the safety net out the window
> in my own code.
It's hard to argue with the truth; but I'll try anyway. Are you going to
provide named constants for each and every literal? Obviously not. And
literals require precisely the ugly suffix to carry their type information.
> It makes it much more difficult to write generic code that use the
> constants;
And, of course, providing only a generic interface (at any cost) makes it
more difficult to write non-generic code.
Note that I could use any of the proposed alternatives; I can make a generic
layer on top of the simple constants if I need it; I can make a non-generic
layer on top of a generic layer, too. So it's not that I'm trying to "lobby"
for the simpler solution. I'm simply trying to present anoher point of
view - the point of view of those that don't write generic code but simply
need to use 'pi.'
-- Peter Dimov Multi Media Ltd.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk