- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个让我彻底糊涂了。在下面的示例中,我得到了错误:
error C2664: 'void std::unique_ptr<_Ty>::swap(std::unique_ptr<_Ty> &&)' : cannot convert parameter 1 from 'const std::unique_ptr<_Ty>' to 'std::unique_ptr<_Ty> &&'
我完全不知道它是如何在我的交换函数中结束的,也不知道它为什么会成为问题。
有趣的是,如果我更改
如果我更改了 void swap(const one& other)
的签名并将常量删除为
void swap(one& other)
一切正常。
void swap(const one& other)
的签名并将常量删除为 void swap(one& other)
它在 VS2010 中编译但在 GCC 中仍然损坏。如果没有交换过载,则没有问题。
//-----------------------------------------------------------------------------
class one
{
public:
one(){}
one(one&& other) : x(std::move(other.x)) {}
one& operator=(one&& other){ x = std::move(other.x); return *this; }
void swap(const one& other){ x.swap(other.x); }
void swap(one&& other){ x.swap(std::move(other.x)); }
private:
one(const one&);
one& operator=(const one&);
std::unique_ptr<int> x;
};
//-----------------------------------------------------------------------------
void swap(one& left, one& right)
{
left.swap(right);
}
//-----------------------------------------------------------------------------
void swap(one&& left, one& right)
{
right.swap(std::move(left));
}
//-----------------------------------------------------------------------------
void swap(one& left, one&& right)
{
left.swap(std::move(right));
}
//-----------------------------------------------------------------------------
class two
{
public:
two(){}
two(two&&){}
two& operator=(two&&){ return *this; }
operator one(){return one();}
private:
two(const two&);
two& operator=(const two&);
};
//-----------------------------------------------------------------------------
int main()
{
std::vector<two> twos(10);
std::vector<one> ones(std::make_move_iterator(twos.begin()), std::make_move_iterator(twos.end()));
}
编辑:非恒定性要求是有道理的。完全是我的疏忽。为什么它首先调用交换?
(供引用,我使用的是 VS2010)
最佳答案
// other can't be const since swap modifies it
void swap(one& other){ x.swap(other.x); }
// Why swapping? (swap on rvalues don't work either since it's pointless.)
//Just move the other
void swap(one&& other){ x = std::move(other.x); }
关于c++ - 为什么我的交换方法会干扰 make_move_iterator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14486236/
这个让我彻底糊涂了。在下面的示例中,我得到了错误: error C2664: 'void std::unique_ptr::swap(std::unique_ptr &&)' : cannot con
我有一个问题: 当我尝试通过vector算法将数据从(带有unique_ptr的list)移至(带有unique_ptr的std::copy)时,虽然成功,但是却在运行中令人迷惑!这是一个代码: st
我有一个 vector 的 vector ,我想将它们一个接一个地连接起来形成一个长 vector 。这可以通过在末尾插入来完成。灵感来自 this question ,我在想,使用 make_mov
从 C++11 开始,要将一些 vector y 附加到另一个 vector x,您可以这样做: x.insert(x.end(), std::make_move_iterator(y.begin()
有人可以向我解释为什么编译失败吗: #include #include #include #include #include template std::unordered_set Fail
我期待 std::make_move_iterator总是会 move 内容,但似乎不会。 看起来是在vector中 move 元素但不在vector . 请看下面的代码片段: #include #
我是一名优秀的程序员,十分优秀!