This code works for dividing each element of a boost::array<> by a value of
its element type:
template <typename T, size_t S>
inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs, const T&
rhs ) {
std::transform( lhs.begin()
, lhs.end()
, lhs.begin()
, std::bind2nd( std::divides<T>(), rhs ) );
return lhs;
}
I want to use std::transform in a similar fashion to set each element of a
boost::array<> to the result of applying std::abs() to it.
The result can easily be obtained with this code:
template <typename T, size_t Order_S>
inline boost::array<T, Order_S>& abs( boost::array<T, Order_S>& v ) {
for ( size_t i = 0; i < v.size(); i++ ) {
v[ i ] = std::abs( v[ i ] );
}
return v;
}
I would like to know how to apply a function such as std::abs in a way
similar to the use of std::bind2nd ( std::divides<T>(), rhs ) in the above
example. How can this be done?

"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true."  Bertrand
Russell 4 3625
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message std::transform( lhs.begin() , lhs.end() , lhs.begin() , std::bind2nd( std::divides<T>(), rhs ) );
I want to use std::transform in a similar fashion to set each element of a boost::array<> to the result of applying std::abs() to it.
Here is one way
int (*absolute)(int) = &std::abs;
std::transform(lhs.begin()
, lhs.end()
, lhs.begin()
, absolute );
Steven T. Hatton wrote in news:AK********************@speakeasy.net in
comp.lang.c++: This code works for dividing each element of a boost::array<> by a value of its element type:
template <typename T, size_t S> inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs, const T& rhs ) { std::transform( lhs.begin() , lhs.end() , lhs.begin() , std::bind2nd( std::divides<T>(), rhs ) ); return lhs; }
I want to use std::transform in a similar fashion to set each element of a boost::array<> to the result of applying std::abs() to it.
The result can easily be obtained with this code:
template <typename T, size_t Order_S> inline boost::array<T, Order_S>& abs( boost::array<T, Order_S>& v ) { for ( size_t i = 0; i < v.size(); i++ ) { v[ i ] = std::abs( v[ i ] ); } return v; }
I would like to know how to apply a function such as std::abs in a way similar to the use of std::bind2nd ( std::divides<T>(), rhs ) in the above example. How can this be done?
Write you're own functor:
template < typename T > struct my_abs
{
typedef T return_type;
T operator () ( T const &arg ) const
{
return std::abs( arg );
/* or even better (ADL friendly version):
*/
using std::abs;
return abs( arg );
}
};
The other alternative is to put a cast into the call to
std::transform:
std::transform(
lhs.begin(), lhs.end(), lhs.begin(),
static_cast< T (*)(T) >( std::abs )
);
The disadvantage here is that for T = short (for example) there
is no overload short std::abs( short ), so the static_cast can't
succeed. Also Argument Dependant Lookup (ADL) can't be used.
Remember to include <cstdlib> and <cmath> so that you get all the
overloads of std::abs, also unless you use an ADL friendly functor
you'll need to include <complex> before calling std::abs if you want
your code to work with std::complex (this also applies valarray).
Rob.
 http://www.victimprime.dsl.pipex.com/
"Rob Williscroft" <rt*@freenet.co.uk> wrote in message template < typename T > struct my_abs { typedef T return_type;
T operator () ( T const &arg ) const { return std::abs( arg );
/* or even better (ADL friendly version): */ using std::abs; return abs( arg ); } };
The standard typedef names are result_type and argument_type. These allow
compatibility with other standard binders in STL and boost like
std::bind2nd.
template < typename T > struct my_abs
{
typedef T argument_type;
typedef T result_type;
T operator () ( T const &arg ) const
{
using std::abs;
return abs( arg );
}
};
My preferred way is to derive from std::unary_function<argument_type,
result_type>.
template < typename T > struct my_abs : std::unary_function<T, T>
{
T operator () ( T const &arg ) const
{
using std::abs;
return abs( arg );
}
};
Siemel Naran wrote in
news:u9**********************@bgtnsc04news.ops.worldnet.att.net in
comp.lang.c++: The standard typedef names are result_type
Thanks I always get this wrong.
and argument_type.
AIUI (I haven't researched this myself, just going on snippets
from usenet) argument_type isn't needed (used).
These allow compatibility with other standard binders in STL and boost like std::bind2nd.
template < typename T > struct my_abs { typedef T argument_type; typedef T result_type; T operator () ( T const &arg ) const { using std::abs; return abs( arg ); } };
My preferred way is to derive from std::unary_function<argument_type, result_type>.
Yep, but that does add an extra byte to the function object in some
cases:
struct my_functor : std::binary_function< int, int, bool >
{
std::less< int > m_less;
// operator here ...
};
The above has 2 subobjects of type std::binary_function< int, int, bool >
so EBO (Empty Base (class) Optimization) can't happen.
I am perhapse being needlessly pedantic (and possibly I'm premeturly
optimizing), OTOH typing "typedef T result_type;" is shorter (and IMO
less cryptic) than writing " : std::unary_function< T, T > ".
If only I could remember result_type not return_type all would be
well :).
Either way I have to lookup result_type or binary_function /
unary_function as I'm actualy not sure the bool return type above
shouldn't be the first argument.
Rob.
 http://www.victimprime.dsl.pipex.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Grahamo 
last post by:
Hi,
I'm implementing a custom iterator (random access type ) so I can use
stl algorithms such as sort on my legacy containers. I'm having
problems compiling however. when implementing my...

by: KS 
last post by:
Hello,
I'm writing some c++ code after a few years with Java and your help is
appreciated. I am getting a core dump on a call to qsort. Can you take
a look at the code and see if there are any...

by: Filimon Roukoutakis 
last post by:
Suppose that we have a function
f(Object*& obj)
and have declared a global std::vector<Object*vec;
Is it valid to do
void g() {
vec.push_back(new Object);

by: phdscholar80 
last post by:
I have the habit of using the scope resolution operator whenever I use
global functions. My premise is that it improves readability by
helping someone who is looking at the code for the first time...

by: VivesProcSPL 
last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many Englishlike terms and syntax in an effort to make it easy to learn, particularly for...

by: jianzs 
last post by:
Introduction
Cloudnative applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...

by: abbasky 
last post by:
### Vandf component communication method one: data sharing
Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...

by: jimatqsi 
last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...

by: fareedcanada 
last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...

by: stefan129 
last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multidomain SSL certificates? Any recommendations on reliable providers or specific...

by: davi5007 
last post by:
Hi,
Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

by: MeoLessi9 
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
 