As juanchopanza noted and as you can see in this question in c++11 this problem no longer exists, so this would probably be the best way to go if it's possible.
If not I came up with a solution that you might be useful in case you have a small amount of types of local classes (e.g comparators and maybe a couple more).
I took the example in the previously mentioned SO question and modified it so it'd work under C++03:
#include <iostream>
#include <vector>
#include <algorithm> // std::remove_if
using namespace std;
template <typename T>
class check {
public:
virtual bool operator()(T x) = 0;
};
template <typename T>
struct cheat {
cheat(check<T> *c) {_c = c;}
bool operator()(T x) {return _c->operator ()(x);}
check<T> *_c;
};
int main() {
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector<int> v( array, array+10 );
class even : public check<int>
{
public:
virtual bool operator()( int x ) { cout<<"Hi"<<endl; return !( x % 2 ); }
};
even e;
remove_if( v.begin(), v.end(), cheat<int>(&e)); // no error, prints Hi
return 0;
}
Also at https://ideone.com/HY5bIU (compiled with Wall,pedantic too... )
If for example I'd like to use a lot of unary operators in my code I can create an abstract class so that all my operators would inherit it, and I've created a cheat
class which basically there only to aid with calling a local class.
Edit: Another option with a little smaller imprint:
#include <iostream>
#include <vector>
#include <algorithm> // std::remove_if
using namespace std;
template <typename T>
class check {
public:
virtual bool operator()(T x) = 0;
struct aid{
aid(check *p){ _c = p;}
check *_c;
bool operator()(T x){
return _c->operator ()(x);
}
};
aid retMe(){
return aid(this);
}
};
int main() {
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector<int> v( array, array+10 );
class even : public check<int>
{
public:
virtual bool operator()( int x ) { cout<<"Hi"<<endl; return !( x % 2 ); }
};
even e;
remove_if( v.begin(), v.end(), e.retMe()); // no error
return 0;
}
https://ideone.com/6SZ4UH
Here the aid
class is hidden inside the abstract class itself, but the idea is the same.
It does add some unnecessary code but on the other hand it's not as grotesque as some hacks may be.