Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

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.

like image 874
template boy Avatar asked Apr 01 '13 16:04

template boy


3 Answers

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.

like image 72
Drew Dormann Avatar answered Nov 14 '22 23:11

Drew Dormann


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
like image 40
Bill Lynch Avatar answered Nov 14 '22 22:11

Bill Lynch


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

like image 23
richvdh Avatar answered Nov 14 '22 21:11

richvdh