jeudi 29 janvier 2015

How to do achieve "const" and "non-const" overloading without duplicated codes?


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.


Aucun commentaire:

Enregistrer un commentaire