You can parametrize the difference. Something like this:
Code:
#include <iostream>
#include <algorithm>
#include <functional>
const int ALPHA_OPAQUE = 1;
struct CSprite
{
int op;
int GetOpacity() const { return op; }
void Draw() const {
std::cout << op << '\n';
}
};
template <class Func>
struct OpaqueTest
{
void operator()(CSprite &sprite)
{
if (Func()(sprite.GetOpacity(), ALPHA_OPAQUE))
{
sprite.Draw();
}
}
};
int main()
{
const int Size = 4;
CSprite m_Sprites[Size] = {{1}, {0}, {1}, {3}};
std::for_each(m_Sprites, m_Sprites + Size, OpaqueTest<std::equal_to<int> >());
std::for_each(m_Sprites, m_Sprites + Size, OpaqueTest<std::not_equal_to<int> >());
}
Personally I find that for_each does so little for you, that the trouble is not often worth it. (C++0x has auto keyword which makes writing the manual loop easier, as well as range-based for loop to begin with. So far I mostly use BOOST_FOREACH in more complicated cases.)