#include <iostream>
#include <future>
#include <chrono>
using namespace std;
using namespace std::chrono;
int sampleFunction(int a)
{
return a;
}
int main()
{
future<int> f1=async(launch::deferred,sampleFunction,10);
future_status statusF1=f1.wait_for(seconds(10));
if(statusF1==future_status::ready)
cout<<"Future is ready"<<endl;
else if (statusF1==future_status::timeout)
cout<<"Timeout occurred"<<endl;
else if (statusF1==future_status::deferred)
cout<<"Task is deferred"<<endl;
cout<<"Value : "<<f1.get()<<endl;
}
Output -
Timeout occurred
Value : 10
In above example, I was expecting future_status to be deferred instead of timeout. sampleFunction has been launched as launch::deferred. Hence it will not be executed until f1.get() has been called. In such condition wait_for should have returned future_status::deferred and not future_status::timeout.
Appreciate if someone can help me understand this.
I am using g++ version 4.7.0 on fedora 17.
GCC and the GNU STL have no support of the complete C++ 11.
Here you can check out the C++ 11 implementation status in GCC and GNU STL:
http://gcc.gnu.org/projects/cxx0x.html
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
Also, read this discussion thread: http://blog.gmane.org/gmane.comp.gcc.bugs/month=20120201
Related
I am setting up my MacBook for C++20 and having problem to compile the code. I have installed latest Xcode, llvm and gcc. Here is the code that I am trying to compile
#include <chrono>
#include <experimental/coroutine>
#include <future>
#include <iostream>
using namespace std;
generator<int> getInts(int first, int last) {
for (auto i = first; i <= last; ++i) {
co_yield i;
}
}
int main() {
for (auto i : getInts(5, 10)) {
std::cout << i << " ";
}
}
however I am getting following error:
$ g++ gen.cpp -std=c++2a
In file included from gen.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/experimental/coroutine:66:5: warning:
<experimental/coroutine> cannot be used with this compiler [-W#warnings]
# warning <experimental/coroutine> cannot be used with this compiler
^
appreciate any insight how to resolve this compile issue.
The Coroutines TS has not been voted into C++20 (indeed, there have been three separate votes, with approval not achieving consensus in all of them), so even if GCC implemented it (which it doesn't), it wouldn't be activated by the -std=c++2a switch.
Now, Clang does implement the Coroutines TS, which you have to turn on with -fcoroutines-ts. And apparently, you have to be using libc++.
I've installed MinGW(gcc 6.3.0) and Eclipse 4.7 on Windows 8.1. I could compile and run other programs without any issue. But when I was using thread it is showing that thread was not declared in this scope. I've tried this and this but both no avail. Could somebody help me fix this?
Here is the code I've treid.
#include <iostream>
#include <thread>
void foo(){
std::cout<<"thread function \n";}
int main(void)
{
std::thread t1(foo);
t1.join();
return 0;
}
I'm not sure about the C++11 features. I've tried Lambda function example from this page(last example) and it is working fine.
#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?
I'm having some very basic trouble with semaphores.
#include <iostream>
using namespace std;
#include <semaphore.h>
int main() {
sem_t sem;
sem_init(&sem, 1, 1);
sem_wait(&sem);
sem_wait(&sem);
printf("Return value: %d\n", sem_wait(&sem));
return 0;
}
I expect this code to wait forever (due to the second sem_wait).
Instead, it prints out "Return value: -1", and exits.
I am compiling with g++ version 4.2.1 on a Mac (OSX 10.8.5).
OS/X does not support unnamed POSIX semaphores. Use sem_open() instead of sem_init().
C++ returns invalid value in the following code:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
205960
When I commnet line after return, it works fine:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
//v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
1
I am using code::blocks with mingw32-g++.exe compiler. The mingw version is: gcc version 4.4.1 (TDM-2 mingw32).
Your compiler has a bug. Fortunately, it is also obsolete. You should upgrade — G++ is up to version 4.6.2, which also implements much of C++11, which is very useful.
If you choose to stick with an older compiler, that is also a decision to accept its flaws.
Edit: If you are really stuck with 4.4 (for example due to a PHB), that series is still maintained. You can upgrade to GCC 4.4.6, released just this past April.