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?
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).
This behavior is a known bug with Eclipse CDT version 10.0.
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/
Help
--> Install New Software...
--> Work with:
and install all the options.Project
--> C/C++ Index
--> Rebuild
.