std :: vector使用其值类型的赋值运算符push_back元素? [英] Does std::vector use the assignment operator of its value type to push_back elements?

查看:209
本文介绍了std :: vector使用其值类型的赋值运算符push_back元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是,为什么?为什么不使用值类型的复制构造函数?

If so, why? Why doesn't it use the copy constructor of the value type?

我得到以下错误:

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc: In member functio
n `ClassWithoutAss& ClassWithoutAss::operator=(const ClassWithoutAss&)':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238:   instantiate
d from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato
r<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =
ClassWithoutAss, _Alloc = std::allocator<ClassWithoutAss>]'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:564:   instantia
ted from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Class
WithoutAss, _Alloc = std::allocator<ClassWithoutAss>]'
main.cpp:13:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238: error: non-st
atic const member `const int ClassWithoutAss::mem', can't use default assignment
 operator

在以下代码上运行g ++ main.cpp:

running g++ main.cpp on the following code:

/*
 * ClassWithoutAss.h
 *
 */

#ifndef CLASSWITHOUTASS_H_
#define CLASSWITHOUTASS_H_

class ClassWithoutAss
{

public:
    const int mem;
    ClassWithoutAss(int mem):mem(mem){}
    ClassWithoutAss(const ClassWithoutAss& tobeCopied):mem(tobeCopied.mem){}
    ~ClassWithoutAss(){}

};

#endif /* CLASSWITHOUTASS_H_ */

/*
 * main.cpp
 *
 */

#include "ClassWithoutAss.h"
#include <vector>

int main()
{
    std::vector<ClassWithoutAss> vec;
    ClassWithoutAss classWithoutAss(1);
    (vec.push_back)(classWithoutAss);

    return 0;
}


推荐答案

元素必须是可复制构造的和可复制赋值的,以在标准容器中使用。因此,一个实现可以自由使用任何想要的。

The C++03 standard says elements must be copy-constructible and copy-assignable to be used in a standard container. So an implementation is free to use whichever it wants.

在C ++ 0x中,这些要求是基于每个操作。 (一般来说,元素必须是可移动构造的和可移动赋值的。)

In C++0x, these requirements are put on a per-operation basis. (In general, elements must be move-constructible and move-assignable.)

为了得到你想要的,你应该使用一个聪明的指针,如 shared_ptr (来自Boost,TR1或C ++ 0x),并完全停用复制功能:

To get what you want, you should use a smart pointer like shared_ptr (from either Boost, TR1, or C++0x), and completely disable copy-ability:

class ClassWithoutAss
{
public:
    const int mem;

    ClassWithoutAss(int mem):mem(mem){}
    // don't explicitly declare empty destructors

private:
    ClassWithoutAss(const ClassWithoutAss&); // not defined
    ClassWithoutAss& operator=(const ClassWithoutAss&); // not defined
};

typedef shared_ptr<ClassWithoutAss> ptr_type;

std::vector<ptr_type> vec;
vec.push_back(ptr_type(new ClassWithoutAss(1)));

指针可以复制得很好,智能指针确保不会泄漏。在C ++ 0x中,你可以使用 std :: unique_ptr 来做到最好,利用移动语义。 (你实际上不需要共享语义,但在C ++ 03它是最简单的,因为它是)。

Pointers can be copied just fine, and the smart pointer ensures you don't leak. In C++0x you can do this best with a std::unique_ptr, taking advantage of move-semantics. (You don't actually need shared semantics, but in C++03 it's easiest as it stands.)

这篇关于std :: vector使用其值类型的赋值运算符push_back元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆