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).
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:
Enter the above URL in Help --> Install New Software... --> Work with: and install all the options.
Restart Eclipse.
Select Project --> C/C++ Index --> Rebuild.
Related
This needs little explanation, but I'm expecting to get an ambiguity error from the following C++ code, however my compiler gives me different results that are apparently not part of the standard.
Environment:
Windows
Clang++ version 12.0.0
Clang++ target = x86_64-pc-windows-msvc
#include <iostream>
void print(int x) {
std::cout << "INT\n";
}
void print(double d) {
std::cout << "DOUBLE\n";
}
int main() {
print(5l); // 5l is type long, should cause ambiguity
}
output:
INT
Any idea why this is happening? My compiler is choosing the function taking an int for some reason instead of issuing an ambiguity error since it shouldn't be able to resolve the function call. Is there some conversion logic that I missed? Do I need to turn up some 'error levels' or something along those lines? Or is this a bug in the compiler? Thanks.
Turned out I had to add -fno-ms-compatibility to my clang compiler flags to switch off MSVC compatibility.
The code below sorting in Visual Studio successfully.
But, in Ubuntu GCC 4.4.7 compiler throws error. It seems it's not familiar with this type of syntax.
How shall I fix this line to make code working in GCC? (the compiler is remote. So I can't upgrade the GCC version either).
What I'm doing right here is:sorting Vector B elements regarding their fitness values
//B is a Vector of class Bird
//fitness is a double - member of Bird objects
vector<Bird> Clone = B;
sort(Clone.begin(), Clone.end(), [](Bird a, Bird b) { return a.fitness> b.fitness; });
//error: expected primary expresssion before '[' token
//error: expected primary expresssion before ']' token...
(Note: this 3 piece of lines compiling successful in MSVC but not in GCC)
my answer is
bool X_less(Bird a, Bird b) { return a.fitness > b.fitness; }
std::sort(Clone.begin(), Clone.end(), &X_less);
It seems to work. Is it a function or not? I don't know its technical name, but it seems to work. I am not much familiar with C++.
You will need to upgrade your C++ as 4.4 is too old to support Lambda. I have Gcc 4.8, but it still requires you enable c++11 that includes lambda functions, so
$ g++ -std=c++11 x.cc
compiles this fine
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
int main()
{
vector<int> Clone;
sort(Clone.begin(), Clone.end(), [](int a, int b) { return a> b; });
}
but still gives errors without -std=c++11 option
I have tried to get following sample code working to know whether asynchronous programming is working in Android NDK. Though NDK has the STL <future> which gets recognized as a header, the std::async is not getting recognized are not getting recognized. The code I tried to use was the following:
#include <future>
#include <iostream>
struct Foo
{
Foo() : data(0) {}
void sum(int i) { data +=i;}
int data;
};
int main()
{
Foo foo;
auto f = std::async(&Foo::sum, &foo, 42);
f.get();
std::cout << foo.data << "\n";
}
Also all the include paths have been set to the specified folder under Properties->Paths and Symbols
Errors
Description Resource Path Location Type
invalid use of incomplete type 'std::__async_sfinae_helper<void (Foo::*)(int), void (Foo::*)(int), Foo*, int>::type {aka struct std::future<void>}' Sample.cpp /Project12/jni line 50 C/C++ Problem
Description Resource Path Location Type
declaration of 'std::__async_sfinae_helper<void (Foo::*)(int), void (Foo::*)(int), Foo*, int>::type {aka struct std::future<void>}' Project12 line 111, external location: D:\android-ndk-r8e-windows-x86_64\android-ndk-r8e\sources\cxx-stl\gnu-libstdc++\4.6\include\future C/C++ Problem
Curently Android NDK does not incorporate all of the C++11 features. Clang 3.3 compiler from NDK r9b is C++11-feature complete, however, STL and stdlib on Android are not.
To use the most recent C++11 feature set in Android use Clang 3.3 compiler from Android NDK r9b. Put this line into your Application.mk file:
NDK_TOOLCHAIN_VERSION := clang
Also, add -std=c++11 switch to the LOCAL_CPPFLAGS variable:
LOCAL_CPPFLAGS += -std=c++11
I'm having trouble working with lambda functions in the Intel compiler, in particular, the following code won't compile:
template<typename T>
std::function<T (int)> make_func(T x) {
return [=](int index) -> T
{
return x;
};
}
The error I get is
error: namespace "std" has no member "function"
The code compiles and runs fine on my Mac, (macports gcc version 4.5). The error is at work, where we use the Intel compiler version 11.1. It does accept lambda functions (with the -std=c++0x option), such as:
auto lam = [=](int j) -> int {
printf("testing for lambdas: %d\t%d\n", n, j);
return n;
};
int g = lam(7);
The version of gcc installed at work is 4.1.2, so I'm guessing that the standard library is old?
/bin/libc.so.6
says it's version 2.5 compiled with gcc 4.1.2.
Is there a way around this?
thanks in advance for any help
I get the same behavior with icc 11.1 on a system where gcc 4.5.2 is installed.
g++'s header <functional> is protected with #ifdef __GXX_EXPERIMENTAL_CXX0X__ which is not defined when icc is used.
I would consider switching to boost::function in this setup, which of course works with icc.
Well, the code shown doesn't include a single header. And yet you refer to the standard library std::function.
So no, it doesn't compile. As with any other part of the standard library, you need to include the header where std::function is defined: <functional>.
Here is a code snippet:
class test {
public:
test(){
cout<<"I am in default constructor ";
}
static void func(const test &obj){
cout<<"I am in function ";
}
protected:
test( const test &o){
cout<<"I am in copy constructor ";
}
};
int main()
{
test::func(test());
}
The above code gives following error with g++ 3.4.6 (on Red Hat Linux) on compilation:
In function `int main()':
error: `test::test(const test&)' is protected
error: within this context
However if you compile with g++ 3.3.2 or g++ 4.4.3 or any other g++ version (on Red Hat Linux), it compiles successfully and gives following output:
I am in default constructor I am in function
In the above code, I have passed the temporary object (created by default constructor) to function func by reference. So why the compiler 3.4.6 is calling the copy constructor?
Most likely because older g++ versions (and I believe it stands for other compilers) were not fully c++ compliant and had more bugs then the current version. As you said, this works on a later version, therefore it is most likely fixed.
EDIT
by the way, have you tried changing compiler settings? Different optimization levels might have different bugs.