相关疑难解决方法(0)

使用placement new来更新引用成员?

以下代码在C++中是否合法?

template<typename T>
class Foo {
public:
    Foo(T& v) : v_(v) {}

private:
    T& v_;
};

int a = 10;
Foo<int> f(a);

void Bar(int& a) {
    new (&f)Foo<int>(a);
}
Run Code Online (Sandbox Code Playgroud)

引用不应该被绑定两次,对吧?

c++ reference placement-new object-lifetime reference-binding

3
推荐指数
1
解决办法
307
查看次数