2011-06-15 41 views
2

是否可以使用本地類作爲std :: find_if的謂詞?您可以使用函數本地類作爲謂詞find_if?

#include <algorithm> 

struct Cont 
{ 
    char* foo() 
    { 

     struct Query 
     { 
      Query(unsigned column) 
       : m_column(column) {} 

      bool operator()(char c) 
      { 
       return ...; 
      } 

      unsigned m_column; 
     }; 

     char str[] = "MY LONG LONG LONG LONG LONG SEARCH STRING"; 

     return std::find_if(str, str+45, Query(1)); 
    } 
}; 

int main() 
{ 
    Cont c; 
    c.foo(); 

    return 0; 
} 

我得到GCC以下編譯器錯誤:

error: no matching function for call to 'find_if(char [52], char*, Cont::foo()::Query)' 
+1

每個人都可以看到你的編輯歷史,所以沒有必要寫在你的文章的額外評論說你編輯的內​​容。 – 2011-06-15 14:19:48

回答

10

在C++ 03中這是不允許的。 本地(不嵌套)類不能是模板參數。 在C++ 11中是允許的。

一些術語尖端:

嵌套類是withing另一個類的範圍中定義的類,例如

class A {class B{};}; 

局部類是功能範圍定義的類(如你的例子)

1

Query是本地類,而不是一個嵌套類。這個問題已被討論in this question