template <typename T, typename Predicate, typename Operation>
void Foo(T& entity, Predicate pred, Operation op)
{
if (pred(entity))
{
op(entity);
}
// and blah
}
template <typename T, typename Predicate, typename Operation>
void Foo(const T& entity, Predicate pred, Operation op)
{
if (pred(entity))
{
op(entity);
}
// and blah
}
P.S.
T& entity
+ pred(const T& entity)
+ op(const T& entity)
is acceptable.
const T& entity
+ pred(T& entity)
+ op(T& entity)
should raise compile error.
Solutions using C++11 is ok.
Examples here:
class MyEntity
{
public:
MyEntity(int e):e(e){}
int e;
};
MyEntity a = 1234;
MyEntity& ra = a;
const MyEntity& cra = a;
auto pred = [](const MyEntity& i)
{
return true;
};
auto cop = [](const MyEntity& i)
{
cout<<i.e<<endl;
};
auto op = [](MyEntity& i)
{
++i.e;
cout<<i.e<<endl;
};
Foo(ra, pred, op); // ok
Foo(ra, pred, cop); // ok
Foo(cra, pred, cop); // ok
Foo(cra, pred, op); // error