使用花括号和括号初始化引用

时间:2014-12-03 20:48:53

标签: c++11 curly-brackets

我遇到了一个让我感到惊讶的问题。我虽然那个

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。

0 个答案:

没有答案
相关问题