你可以使用函数本地类作为find_if的谓词吗?

Gra*_*eme 2 c++

是否可以使用本地类作为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;
}
Run Code Online (Sandbox Code Playgroud)

我在gcc上遇到以下编译器错误:

error: no matching function for call to 'find_if(char [52], char*, Cont::foo()::Query)'
Run Code Online (Sandbox Code Playgroud)

Arm*_*yan 10

在C++ 03中,这是不允许的.本地(非嵌套)类不能是模板参数.在C++ 11中,它是允许的.

一些术语提示:

嵌套类是使用另一个类的范围定义的类,例如

class A {class B{};};
Run Code Online (Sandbox Code Playgroud)

本地类是在函数范围内定义的类(如在您的示例中)