是否可以使用本地类作为 std::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)'
最佳答案
在 C++03 中这是不允许的。 本地(非嵌套)类不能是模板参数。 在 C++11 中是允许的。
一些术语提示:
嵌套类是在另一个类的范围内定义的类,例如
class A {class B{};};
局部类是在函数范围内定义的类(如您的示例)
关于c++ - 可以使用函数局部类作为 find_if 的谓词吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6358068/