Using local classes with STL algorithms

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.

Leave a Comment