我遇到了一个令我惊讶的问题。我虽然那
int i = 3;
int i {3};
int i (3);
是等价的。
这导致我为以下类做(请注意构造函数中的 t {t_} )(来自 Anthony Williams 的 C++ Concurrency in Action 的例子,所以在这里给出信任,这样我就不会违反一些版权问题):
class thread_guard{
std::thread& t;
public:
explicit thread_guard(std::thread& t_) : t {t_} {}
~thread_guard()
{
if(t.joinable())
t.join();
std::cout << "~thread_guard running" << std::endl;
}
};
这给了我一个编译器错误:
error: invalid initialization of non-const reference of type 'std::thread&' from an rvalue of type...
然而,改变
t {t_}
进入
t (t_)
让它工作。这当然让我质疑我对两者等价的假设。
感谢您的任何意见。
编辑:编译器是 g++,Windows 8.1 上的版本 4.8.1。