Valarray
Marc Lepage
mlepage@molecularmining.com
Fri Nov 19 07:58:00 GMT 1999
Gabriel Dos Reis wrote:
>
> Current CVS trees (both GCC and libstdc++-v3) are more up-to-date.
Thanks. I'm using GCC 2.95.x and awaiting stable releases, as I am working on
production code. However, I am on this list to track development, so I know when
to begin planning for upgrade to standard C++ (e.g. string streams). :-)
> | Why doesn't valarray use more macro shenanigans (like _DEFINE_BINARY_OPERATOR(+,
> | plus)) to declare the operators within the class? See bits/std_valarray.h:140
> | for details.
>
> 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(-)
...
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.
> | 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)
> | 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;
};
--
Marc Lepage
Software Developer
Molecular Mining Corporation
http://www.molecularmining.com/
More information about the Libstdc++
mailing list