4

First of all, this is not a dummy question about arrays or operator[] overload!

I was trying to compile Qt Creator and I've received an error in this method:

static QList<IDocumentFactory*> getNonEditorDocumentFactories()
{
    return ExtensionSystem::PluginManager::getObjects<IDocumentFactory>(
        [](IDocumentFactory *factory) {
            return !qobject_cast<IEditorFactory *>(factory);
        });
}

The error is:

mainwindow.cpp:748: error: expected primary-expression before ‘[’ token
mainwindow.cpp:748: error: expected primary-expression before ‘]’ token
mainwindow.cpp:748: error: expected primary-expression before ‘*’ token
mainwindow.cpp:748: error: ‘factory’ was not declared in this scope

I know that I'm doing something wrong to compile Qt Creator, probably g++ version, but the question is not that.

I would like to understand this code because for me this use of [] is syntactically incorrect. Can someone please explain me what is happening here.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Pedro MC
  • 41
  • 1
  • 6
    That's a C++11 lambda: http://en.cppreference.com/w/cpp/language/lambda. – geometrian Sep 09 '14 at 03:16
  • You may need gcc 4.8 or later, if it's a C++11 feature. @Ian, that's probably better as an answer since it answers the specific question ("I would like to understand ..."). Just make sure it's not a link-only answer :-) – paxdiablo Sep 09 '14 at 03:17
  • @paxdiablo; I agree generally, but _I_ don't fancy writing a descriptive answer right now. While someone else does, the OP can start skimming that link (or looking on SO, now that he knows what it is, since C++11 lambda is already well discussed). – geometrian Sep 09 '14 at 03:43
  • Welcome to modern C++. Now you can leave all the Qt-specific hacks behind. – Ben Voigt Sep 09 '14 at 03:54

2 Answers2

4

That's a lambda function. It was introduced in C++11. You can get more details at http://en.cppreference.com/w/cpp/language/lambda.

If you are not in a position to use a lambda function, the equivalent code in C++03 would be:

struct MyFunctor
{
   bool operator()(IDocumentFactory *factory) const
   {
      return !qobject_cast<IEditorFactory*>(factory);
   }
};

static QList<IDocumentFactory*> getNonEditorDocumentFactories()
{
    return ExtensionSystem::PluginManager::getObjects<IDocumentFactory>(MyFunctor());
}

You can use the above in C++11 too but it is more idiomatic to use a lambda function in c++11.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Answering this question:

I would like to understand this code because for me this use of [] is syntactically incorrect. Can someone please explain me what is happening here.

In C++, you can declare lambda functions:

// declare lambda function and assign it to foo
auto foo = [](){
     std::cout << "hello world" << std::endl;
};

foo(); // call foo

What is happening in your code, is that getObjects<>() is being passed a lambda function as an input.

return ExtensionSystem::PluginManager::getObjects<IDocumentFactory>(

    // this is the lambda function
    [](IDocumentFactory *factory) {
        return !qobject_cast<IEditorFactory *>(factory);
    }


); 
aCuria
  • 6,935
  • 14
  • 53
  • 89