开发者

Can you use a function local class as predicate to find_if?

Is it possible to use a local class as a predicate to std::find_if?

#include <algorithm>

struct Cont
{
    char* foo()
    {

        stru开发者_如何学Pythonct 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;
}

I get the following compiler error on gcc:

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


In C++03 this is not allowed. Local(not nested) classes cannot be template parameters. In C++11 it is allowed.

Some terminology tip:

A nested class is a class that is defined withing the scope of another class, e.g.

class A {class B{};};

A local class is class defined in function scope(as in your example)


Query is a local class, not a nested class. This problem has been discussed in this question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜