简体   繁体   中英

Why does this basic thread program fail with Clang but pass in g++?

I have this simple program that works with threads. In Clang I get a bunch of confusing irrelevant errors. Here is the program:

#include <iostream>
#include <thread>
#include <future>

int main()
{
   std::packaged_task<int()> task([] { return 1; });
   std::future<int> result = task.get_future();

   task();

   std::cout << "Result was: " << result.get();
}

Errors:

error: no matching constructor for initialization of 'duration' (aka ' std::chrono::duration<long, std::ratio<1, 1000000> > ') : _ d( _t.time_since_epoch()) note: in instantiation of function template specialization 'std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<long, std::ratio<1, 1000000> > >::time_point<std::chrono::duration<long, std::ratio<1, 1000000000> > >' requested here

There's a lot more, but you can see it in this link of the program. Oddly, it compiles fine in g++ 4.7.3 and 4.6.3. Why is this only happening in Clang?

Update: As David pointed out, it seems only to be failing when I include the <future> header.

This is a documented bug in clang/libstdc++.

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=666539

http://llvm.org/bugs/show_bug.cgi?id=12893

From Clang's status page :

Clang's C++11 mode can be used with libc++ or with gcc's libstdc++, but patches are needed to make libstdc++-4.4 work with Clang in C++11 mode. Patches are also needed to make libstdc++-4.6, and libstdc++-4.7 work with Clang releases prior to version 3.2 in C++11 mode.

This is an incompatibility between libstdc++ and clang. If you were to build against libstdc++ 4.8.0, this problem goes away.

[11:43am][wlynch@apple /tmp] /opt/llvm/3.2/bin/clang++ -std=gnu++11 -gcc-toolchain /opt/gcc/4.8.0 se.cc |& wc -l
0
[11:43am][wlynch@apple /tmp] /opt/llvm/3.2/bin/clang++ -std=gnu++11 -gcc-toolchain /opt/gcc/4.7.2 se.cc |& wc -l
21

If this is happening with clang 3.2, it's not bug 12893 (which was fixed in clang 3.2).

It is more likely to be this bug , which is really a bug in libstdc++ 4.7: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53841

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM