This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Re: Valarray
Marc Lepage <mlepage@molecularmining.com> writes:
[...]
| > Why should them be declared within then class? Where do you draw that
| > requirement from?
|
| I don't know about requirements. My observation is that instead of declaring:
|
| valarray<_Tp>& operator+= (const _Tp&);
| valarray<_Tp>& operator-= (const _Tp&);
| ...
| valarray<_Tp>& operator+= (const valarray<_Tp>&);
| valarray<_Tp>& operator-= (const valarray<_Tp>&);
| ...
|
| you could automate the declarations using macros, in this fashion:
|
| #define _DECLARE_VALARRAY_AUGMENTED_ASSIGNMENT(_Op)\
| valarray<_Tp>& operator##_Op##= (const _Tp&);\
| valarray<_Tp>& operator##_Op##= (const valarray<_Tp>&);
|
| _DECLARE_VALARRAY_AUGMENTED_ASSIGNMENT(+)
| _DECLARE_VALARRAY_AUGMENTED_ASSIGNMENT(-)
| ...
OK, I see. Believe it or not but I often happen to read the files and
I do want them to be readable, especially when debugging. I just don't
like Cpp abuse.
| This style of macro use is already employed later to actually define the
| operators:
|
| _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(+, plus)
| _DEFINE_VALARRAY_AUGMENTED_ASSIGNMENT(-, minus)
|
| so I wondered why it wasn't used to declare them as well.
Because I intended to minimize Cpp usage and I like readable class
definitions
| > | Similarly, why aren't the function objects defined using macros in the same
| > | fashion? See bits/stl_function.h:49 for details.
| >
| > Please, can you exapnd on this?
|
| The function objects are declared as follows:
|
| template <class _Tp>
| struct plus : public binary_function<_Tp,_Tp,_Tp> {
| _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
| };
|
| template <class _Tp>
| struct minus : public binary_function<_Tp,_Tp,_Tp> {
| _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
| };
|
| I wondered why macros weren't employed as in valarray, like this:
|
| #define _DEFINE_FUNCTIONAL_ARITHMETIC_OPERATION(_Op, _Name)\
| template <class _Tp>\
| struct _Name : public binary_function<_Tp,_Tp,_Tp> {\
| _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x _Op __y;
| }\
| };
|
| _DEFINE_FUNCTIONAL_ARITHMETIC_OPERATION(+, plus)
| _DEFINE_FUNCTIONAL_ARITHMETIC_OPERATION(-, minus)
Valarray was implemented in September 1997. STL already existed.
| > | P.S. Not related to libstdc++-v3, has anyone had any experience wrapping
| > | functors/valarray in more OO fashion, for dynamic binding and persistence? I've
| > | been playing with that recently, and wouldn't mind comparing notes.
| >
| > What do call "more OO fashion" ?
|
| I mean:
|
| class AdaptableUnaryFunction :
| public std::unary_function<std::valarray<double>, std::valarray<double>
| >
| {
| public:
| virtual ~AdaptableUnaryFunction() = 0;
| virtual result_type operator ()(const argument_type& arg) const = 0;
| };
Do you realize the implications?
-- Gaby