When trying to make thread using lambda with following code:
#include <iostream>
#include <thread>
using namespace std;
int main() {
thread t([] {
cout << "hello\n";
});
t.join();
return 0;
Eclipse says:
Description Resource Path Location Type
Invalid arguments '
Candidates are:
thread(const std::thread &)
thread(std::thread &&)
' cpptest.cpp /cpptest/src line 14 Semantic Error
Code compiles successfully, but that error is annoying, I also tried C/C++ Build > Settings > GCC C++ Compiler > Dialect > Language Standard = ISO C++17, but without luck.
There may be problem with cpp checking setting, which are not set to higher cpp version, but I can't see that option in project setting.
Related
I was trying to use shared pointers in CUDA by using NVIDIA's version of the C++ standard library. So tried to include <cuda/std/detail/libcxx/include/memory>. I got some errors. One of them includes cannot open source file with <__config> and <__functional_base>. Those files were clearly in the directory. It's like visual studios acts like those files don't exist even though they do. Another error I get is linkage specification is incompatible with previous "" with <cmath>.
I did little digging. I found out that cannot open source file is apparent with every non-header file that starts with _ in cuda/std/detail/libcxx/include/. It is like Visual Studio somehow acts like those files don't exist despite being clearly located in the additional include directories. Furthermore, when I type cuda/std/detail/libcxx/include/, IntelliSense won't find these files. If I can get visual studio to recognize those files, I can properly include any files in NVIDIA's version of standard library.
The first thing to understand is that CUDA doesn't have a C++ standard library. What you are referring to is the libcu++, which is an extremely bare bones heterogenous reimplementation of a tiny subset of what is defined in the C++ standard library. You can use whatever is defined in libcu++ (and that is not much, it is a very incomplete implementation) as follows:
Prepend the local path cuda/std/ to whatever standard library header you are using to substitute the import from the native host C++ standard library to libcu++
Change the namespace from std to cuda::std
compile using nvcc
As a simple example:
$ cat atomics.cpp
#include <iostream> // std::cout
#include <cuda/std/atomic> // cuda::std::atomic, cuda::std::atomic_flag, ATOMIC_FLAG_INIT
#include <thread> // std::thread, std::this_thread::yield
#include <vector> // std::vector
cuda::std::atomic<bool> ready (false);
cuda::std::atomic_flag winner = ATOMIC_FLAG_INIT;
void count1m (int id) {
while (!ready) { std::this_thread::yield(); } // wait for the ready signal
for (volatile int i=0; i<1000000; ++i) {} // go!, count to 1 million
if (!winner.test_and_set()) { std::cout << "thread #" << id << " won!\n"; }
};
int main ()
{
std::vector<std::thread> threads;
std::cout << "spawning 10 threads that count to 1 million...\n";
for (int i=1; i<=10; ++i) threads.push_back(std::thread(count1m,i));
ready = true;
for (auto& th : threads) th.join();
return 0;
}
$ nvcc -std=c++11 -o atomics atomics.cpp -lpthread
$ ./atomics
spawning 10 threads that count to 1 million...
thread #6 won!
Note that as per the documentation, there are presently (CUDA 11.2) only implementations of:
<atomic>
<latch>
<barrier>
<semaphore>
<chrono>
<cfloat>
<ratio>
<climits>
<cstdint>
<type_traits>
<tuple>
<functional>
<utility>
<version>
<cassert>
<cstddef>
with complex support coming in the next CUDA release from the looks of things.
You mentioned shared pointers. There is no <memory> implementation at present, so that cannot be made to work.
I'm getting an error of invalid arguments in the Eclipse IDE for C/C++ Developers Photon (4.8.0) at map_name.insert(make_pair("string_name", int_name);.
I am using GCC 8.2.0. I'm trying some simple stuff with STL.
Tried either both insert(make_pair()) or insert(pair<string, int>()) getting same IDE error(Semantic error). Why is that?
Code:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> ages;
ages["Mike"] = 21;
ages["Johnny"] = 20;
ages["Vicky"] = 30;
ages["Mike"] = 42;
// ages.insert(make_pair("Peter", 100));
ages.insert(pair < string, int > ("Peter", 100));
for(map<string, int>::iterator it = ages.begin(); it!=ages.end(); it++)
{
cout<< it->first<<": "<< it->second<<endl;
}
return (0);
}
This is the error that is displayed in the IDE:
The standard library implementation that ships with GCC 8 uses a type trait intrinsic called __is_constructible, which Eclipse CDT's parser does not yet support.
This can result in false positive errors when CDT is made to parse GCC 8's standard library code.
If you use GCC 7 or earlier, you don't get any errors for this code.
UPDATE: This eclipse bug tracks adding support for __is_constructible to CDT's parser. It has recently been fixed, though the fix has not appeared in a CDT release yet.
#include <iostream>
#include <string>
using namespace std;
int main(){
string s = "wassup", newVal = "though";
cout << *(s.insert(s.begin(), newVal.begin(), newVal.end()));
}
This brings up the problem that the return type of string's insert member function is void (error: void value not ignored as it ought to be). This link indicates C++98 returns void but the "new" standard C++11 does indeed return an iterator.
A bit of context, I actually faced this problem earlier. I was/am using CodeBlocks (GCC Compiler Collection) on Windows 7 64-bit and this program gave the same issue (dereferencing void):
#include <iostream>
#include <list>
int main(){
list<int> x = {1,2,3,4};
*(x.insert(++x.begin(), 3, 2));
for(auto c : x)
cout << c;
}
I posted my issue on a different forum and a user pointed out Mingw32 was missing that particular C++11 change, indicating Mingw-w64 does not have this issue. So I went straight to installing Mingw-w64 on Code::Blocks using this guide and the problem was resolved. It's only now I've found out that the overloaded function which takes three iterator parameters STILL returns void.
I'm a little confused as to why Code::Blocks Mingw32 didn't supply a fully updated C++11 standard. Can anyone suggest a download that will definitely bring my compiler up to speed?
my thread program is :
#include<iostream>
#include<thread>
using namespace std;
void t()
{
cout<<"from thread\n";
}
int main()
{
thread i(&t);
cout <<"from main\n";
i.join();
}
but it shows following error in codeblocks:
1)'thread ' was not declared in this scope
2)expected ';' before 'i'
3)'i' was not declared in this scope
How can I solve it?I am using windows and codeblocks 12.11
I have the same problem. Unfortunately the version of GCC Code::Blocks uses does not support the features of C++11 that you want. Turning on the option -std=c++0x will only ugrade the compiler to a less recent version of the new Standard. That means you will only enable basic support of C+11.
My thread program is:
#include<iostream>
#include<thread>
using namespace std;
void t()
{
cout<<"from thread\n";
}
int main()
{
thread i(&t);
cout <<"from main\n";
i.join();
}
but it shows following error in codeblocks:
1)'thread ' was not declared in this scope
2)expected ';' before 'i'
3)'i' was not declared in this scope
How can I solve it?I am using windows and codeblocks 12.11
First, are you on windows or linux?
If you are on linux, you must compile with C++11 support. Just pass -std=c++11 to g++.
I can't help you with windows.
Your IDE may not support C++11 yet. since thread is included in the standard since C++11. See this thread for CodeBlocks? http://forums.codeblocks.org/index.php?topic=15536.0