我有以下代码,它可以很好地编译GCC 4.8.4,GCC 5.2.0和clang 3.8.0 trunk(带-std = c ++ 11)
#include <utility>
#include <thread>
struct Callable {
static void run() {}
};
void start(Callable && c)
{
std::thread t(&c.run); // construct thread
}
int main()
{
Callable c;
start(std::move(c));
}
(在左值上使用std :: move不是犹太洁食,但这不是重点)。
但是,如果我将线程构造函数调用更改为
std::thread t(c.run);
clang仍在编译它,但GCC 5.2.0抱怨:
/home/bulletmagnet/workspace/zarquon/prog/msyncd/src/new_thread_test.cc: In function ‘void start(Callable&&)’:
/home/bulletmagnet/workspace/zarquon/prog/msyncd/src/new_thread_test.cc:10:24: error: no matching function for call to ‘std::thread::thread(void())’
std::thread t(c.run);
^
In file included from /home/bulletmagnet/workspace/zarquon/prog/msyncd/src/new_thread_test.cc:2:0:
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/include/c++/thread:133:7: note: candidate: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void(); _Args = {}]
thread(_Callable&& __f, _Args&&... __args)
^
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/include/c++/thread:133:7: note: no known conversion for argument 1 from ‘void()’ to ‘void (&&)()’
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/include/c++/thread:128:5: note: candidate: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/include/c++/thread:128:5: note: no known conversion for argument 1 from ‘void()’ to ‘std::thread&&’
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/include/c++/thread:122:5: note: candidate: std::thread::thread()
thread() noexcept = default;
^
/usr/local/lib/gcc/x86_64-unknown-linux-gnu/5.2.0/include/c++/thread:122:5: note: candidate expects 0 arguments, 1 provided
(GCC 4.8.4也抱怨)。
那么,哪个编译器是对的?
答案 0 :(得分:1)
我认为 Clang是对的,EDG也接受了代码,所以我打开了https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68386
明显的解决方法是不要使用这种奇怪的语法来引用静态成员函数,而是将其称为Callable::run
,它可以使用或不使用&
运算符。