Using local classes with STL algorithms
Posted
by David Rodríguez - dribeas
on Stack Overflow
See other posts from Stack Overflow
or by David Rodríguez - dribeas
Published on 2009-04-12T23:11:18Z
Indexed on
2010/05/15
21:04 UTC
Read the original article
Hit count: 404
I have always wondered why you cannot use locally defined classes as predicates to STL algorithms.
In the question: Approaching STL algorithms, lambda, local classes and other approaches, BubbaT mentions says that 'Since the C++ standard forbids local types to be used as arguments'
Example code:
int main() {
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector<int> v( array, array+10 );
struct pair : public std::unary_function<int,bool>
{
bool operator()( int x ) { return !( x % 2 ); }
};
std::remove_if( v.begin(), v.end(), pair() ); // error
}
Does anyone know where in the standard is the restriction? What is the rationale for disallowing local types?
© Stack Overflow or respective owner