关于临时对象,右值参数和push_back()(改)
class test {
public :
test() {
a = 100 ;
}
private :
int a ;
} ;
void main() {
deque<class test> container ;
container.push_back(class test()) ; //报错
class test t1 ;
container.push_back(t1) ; //正常
}
我参考了sgi的stl手册,得知push_back的参数为(const T&)
在报那一行里,临时的test对象不正是const 对象吗?为何对右值的引用无法正确传入push_back?
注意,这个做法在VS的编译器里貌似可行,但我昨晚用G++就不行了
我想知道为什么右值传不进去?请教原因,谢谢
相关推荐
c++push_back 以及 emplace_back 的区别
区别 都说emplace快push慢,今天就详细研究下到底两者有什么区别,以及这个move在中间扮演了一个什么角色。 先上测试代码,是一个自己手写的str字符串类,几种构造函数,都写全了: class str { public: friend ostream& operator <<(ostream& out, const str& p); str() :data(nullptr), len(0) {} //构造函数 str(const char* p) { len = strlen(p); data = new char[len
C++ push方法与push_back方法的使用与区别
主要介绍了C++ push方法与push_back方法的使用与区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
C++ 中"emplace_back" 与 "push_back" 的区别
主要介绍了C++ 中"emplace_back" 与 "push_back" 的区别的相关资料,需要的朋友可以参考下
C++ 中”emplace_back” 与 “push_back” 的区别
C++ 中”emplace_back” 与 “push_back” 的区别 emplace_back和push_back都是向容器内添加数据. 对于在容器中添加类的对象时, 相比于push_back,emplace_back可以避免额外类的复制和移动操作. “emplace_back avoids the extra copy or move operation required when using push_back.” 参见: http://en.cppreference.com/w/cpp/container/vector/emplace_back 注意下面代码中的emplac
C++ 中push_back方法得使用
介绍c++中push_back方法的使用,包括vector中和string中,使用很方便也很广泛


