Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using local classes with STL algorithms

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 even : public std::unary_function<int,bool>    {       bool operator()( int x ) { return !( x % 2 ); }    };    std::remove_if( v.begin(), v.end(), even() ); // error } 

Does anyone know where in the standard is the restriction? What is the rationale for disallowing local types?


EDIT: Since C++11, it is legal to use a local type as a template argument.

like image 683
David Rodríguez - dribeas Avatar asked Apr 12 '09 23:04

David Rodríguez - dribeas


People also ask

What is local class with example?

Local Class in C++A class declared inside a function is known as a local class in C++ as it is local to that function. An example of a local class is given as follows. In the above example, func() is a function and class LocalClass is defined inside the function. So, it is known as a local class.

What is the advantage of STL in C++?

STL provides a range of data structures that are very useful in various scenarios. A lot of data structures are based on real-life applications. It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized.

How many algorithms are there in STL?

The Standard Template Library (STL) is a software library originally designed by Alexander Stepanov for the C++ programming language that influenced many parts of the C++ Standard Library. It provides four components called algorithms, containers, functions, and iterators.


1 Answers

It's explicitly forbidden by the C++98/03 standard.

C++11 remove that restriction.

To be more complete :

The restrictions on types that are used as template parameters are listed in article 14.3.1 of the C++03 (and C++98) standard:

A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

template <class T> class Y { /* ... */  };  void func() {          struct S { /* ... */ }; //local class          Y< S > y1; // error: local type used as template-argument         Y< S* > y2; // error: pointer to local type used as template-argument } 

Source and more details : http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=420

To sum up, the restriction was a mistake that would have been fixed sooner if the standard was evolving faster...

That said today most last versions of common compilers does allow it, along with providing lambda expressions.

like image 186
Klaim Avatar answered Sep 30 '22 22:09

Klaim