1

Update:

These errors appear to be from CODAN, Eclipse's code analyzer, that appear in the Problems view and are also underlined in red on the line the error refers to. Oddly enough building the project successfully creates the executable, and the build does not report these errors in the Console. I can run that executable and get the expected output.

So now the question becomes: how to have Eclipse's CODAN recognize these uses of lambdas and function pointers with std::function are not errors?

Original Question:

In the following C++ code, it compiles fine directly with g++, but causes errors in Eclipse CDT. How can I get my Eclipse project to recognize and allow the use of lambdas (or function pointers) with std::function?

Note: see Edit at bottom of question for update

#include <functional>
#include <iostream>

void foo(int i) {
    std::cout << i << std::endl;
}

void function_ptr(void (*bar)(int)) {
    bar(1);
}

void function_std(std::function<void(int)> baz) {
    baz(2);
}

int main() {
    // these function calls are ok
    function_ptr(&foo);
    function_ptr([](int i) -> void {
        foo(i);
    });

    // these function calls cause errors
    function_std(&foo);
    function_std([](int i) -> void {
        foo(i);
    });

    // these assignments cause errors
    std::function<void(int)> f1 = foo;
    std::function<void(int)> f2 = [](int i) -> void {
        foo(i);
    };

    f1(3);
    f2(3);

    return 0;
}

Compiling this code on the command line works as expected and produces the following output:

$ g++ src/LambdaFunctionParameterExample.cpp -o example
$ ./example
1
1
2
2
3
3

However in Eclipse, the function calls accepting a std::function as a parameter produce this error:

Invalid arguments '
Candidates are:
void function_std(std::function<void (int)>)
'

And the std::function variable assignments produce this error:

Invalid arguments '
Candidates are:
 function()
 function(std::nullptr_t)
 function(const std::function<void (int)> &)
 function(std::function<void (int)> &&)
 function(#10000)
'

I have set the language standard to ISO C++17 (-std=c++17) in Project -> Properties -> C/C++ Build -> GCC C++ Compiler -> Dialect. (I have assumed setting this property was necessary to access the <functional> header according to this documentation. Oddly enough, specifying the language level (or not) is not effecting the above errors. And, specifying the language level is not necessary when building directly with g++.)

I'm using macOS Catalina 10.15.5, gcc 10.2.0 from homebrew, and Eclipse CDT version 2020-09 (4.17.0).

aaroncarsonart
  • 1,054
  • 11
  • 27
  • IDEs like Eclipse CDT generally aren't as good as compilers about correctly working through declarations, templates, etc. – aschepler Dec 08 '20 at 22:51
  • is this an intellisense (or however is called in eclipse) or an actual compile error? – bolov Dec 08 '20 at 22:52
  • 2
    @bolov CODAN. Fortunately there is no CODAN armada from which you must defend the frontier. – user4581301 Dec 08 '20 at 23:22
  • @bolov Thank you for asking for a clarification. These appear to be code analysis errors that appear in the Problems view and also underlined in red on the line the error refers to. Oddly enough the executable is still built, and building the project does not report errors in the Console. I can actually run the executable and get the expected output. (I didn't realize this was the case before, so I'll add this info to the question.) – aaroncarsonart Dec 09 '20 at 00:19
  • 1
    this means it's just the intellisense. Some IDEs use a different engine for realtime autocompletion and error hinting then the actual compiler that is used to build the program. This intellisense can lack behind in language features, can be much more buggy, can use old cache instead of the updated code etc. It is a good help when it works, but when it doesn't don't fret about it, if you can't quickly make it work then ignore it, disable it or chose another IDE that can use the same compiler to build and provide intellisense. – bolov Dec 09 '20 at 00:26
  • Does [this answer](https://stackoverflow.com/a/58567166/1329652) answer your question? – Kuba hasn't forgotten Monica Dec 09 '20 at 00:42
  • @UnslanderMonica I followed the instructions suggested by both answers to that question, but neither led to changes in the errors being reported. – aaroncarsonart Dec 09 '20 at 03:14
  • I found an [Eclipse bug report](https://bugs.eclipse.org/bugs/show_bug.cgi?id=564501) matching my issue exactly. Should I add this as an answer to my question? My intuition says no, as this doesn't answer the question. But, it seems there will be no other answer. Perhaps the question should be closed? – aaroncarsonart Dec 09 '20 at 03:20
  • Following advice found [here](https://meta.stackoverflow.com/questions/319439/what-to-do-when-your-question-is-not-an-unsolved-problem-but-a-bug), I decided to answer my own question. – aaroncarsonart Dec 09 '20 at 04:17

1 Answers1

1

This behavior is a known bug with Eclipse CDT version 10.0.

Update:

This issue is fixed by upgrading to Eclipse CDT version 10.1:
https://download.eclipse.org/tools/cdt/builds/10.1/cdt-10.1.0-rc2/

Steps to fix:

  1. Enter the above URL in Help --> Install New Software... --> Work with: and install all the options.
  2. Restart Eclipse.
  3. Select Project --> C/C++ Index --> Rebuild.
aaroncarsonart
  • 1,054
  • 11
  • 27