50

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.

Community
  • 1
  • 1
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489

2 Answers2

53

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.

Klaim
  • 67,274
  • 36
  • 133
  • 188
  • I know, but I'd like to know where to see if I can understand why. Do you have a reference into the standard? – David Rodríguez - dribeas Apr 12 '09 at 23:16
  • Are you referring to 14.3.1.2, "template type arguments"? – greyfade Apr 12 '09 at 23:19
  • I added some informations and a link that might help. To sum up, the restriction was a mistake that would have been quickly fixed if the standard was evolving faster... – Klaim Apr 12 '09 at 23:23
  • 2
    But as an ISO standard it can't, by the ISO rules, evolve any faster than in 10 year periods. So for example 98 standard existed, so C++0x couldn't come out before 2008, and by the looks of it we won't have a C++1x, but a C++21 or something like that. ISO standards are too slow... – Robert Gould Apr 13 '09 at 00:12
  • ISO rules enforce 10 years periods? Really? If that's true, it's a real pain in the arse. Stroustrup suggested in his faq that the next standard should come in a shorter time... – Klaim Apr 13 '09 at 00:29
  • 8
    Committee members, including Stroustrup, have said that the ten year timeclock was an incorrect interpretation of ISO rules. – Max Lybbert Oct 31 '11 at 17:44
  • Good news. We'll get more infos about the new way they will work at the next meeting I guess. – Klaim Oct 31 '11 at 18:27
  • _That said today all last versions of common compilers does allow it, along with providing lambda expressions._ It still shows up when compiling with gcc 4.9, so not **all versions**. – abergmeier Feb 02 '15 at 12:29
5

The restriction will be removed in '0x, but I don't think you'll be using them very much. And that's because C++-0x is going to have lambdas! :)

int main() {
   int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   std::vector<int> v( array, array+10 );

   std::remove_if( v.begin()
                 , v.end()
                 , [] (int x) -> bool { return !(x%2); })
}

My syntax in the above may be not be perfect, but the general idea is there.

Richard Corden
  • 21,389
  • 8
  • 58
  • 85
  • I knew about lambdas. In fact, what I wanted to achieve is the closest possible with the current standard. Kind of the solution Java proposes. – David Rodríguez - dribeas Apr 14 '09 at 18:21
  • And in which way is this an answer to the actual question (let alone one worth 4 up-votes)? – Christian Rau May 29 '12 at 07:50
  • @ChristianRau: ;) I can see why you'd say this. The question is why are they not allowed and the first line of the answer addresses this ie. it's been removed so there must not have been a good reason for the restriction. At the time I clearly thought lambdas were worth mentioning. Now of course 3 years later lambdas are well known so they're no longer news! – Richard Corden May 29 '12 at 16:39