max() in phobos, and English logic operators
John Reimer
terminal.node at gmail.com
Mon Nov 13 22:06:54 PST 2006
On Wed, 08 Nov 2006 05:48:58 -0800, Sean Kelly <sean at f4.ca> wrote:
> David Qualls wrote:
>> So, is it possible to use templates to define generic binary
>> operators; like 'and' and 'or', or a unary operator like 'not'?
>
> They're in the C++ standard library, so I don't see why not.
>
>> After looking at the mass of code it takes to implement a simple
>> generic max() or min() function in D, I'm really starting to pine
>> for my C preprocessor...
>> #define max(a,b) ((a)>(b)?(a):(b))
>> Yeah, I know it breaks if a or b include side effects, but it's
>> extremely READABLE!
>
> There has been a lot of work done in the C++ community to produce a
> template function that is as functional as this simple macro. In fact,
> I think Andrei actually wrote a series of articles on it. I think we
> can do better in D because of the improved compile-time type
> information, but it's difficult to match the simplicity of macro code
> replacement.
>
>
> Sean
Blech! I do not find C/C++ macros readable! This small sample is hardly
representative of the abuse of the preprocessor that exists in most of
C/C++ land and is hardly an argument for having a PP in D. Abuse of the
CPP has made a horrid mess of code and makes it impossible to determine
what is /really/ going on in there (I especially resent macros in macros
in macros :P ). I, for one, don't miss the preprocessor's absence from
D. D compile-time features are gradually progressing to the state where
almost all CPP functionality is possible, anyway. Those that are addicted
to the CPP might do better to take a break from it and start experimenting
with the D system.
Sure the CPP may inline things efficiently, but it's really just a poor
man's templating system... a hack that's gone horribly wayward since long
ago.
Oh... that feels better. It seems I periodically have to bash the CPP...
at least once a year, at least. :)
-JJR
More information about the Digitalmars-d-learn
mailing list