C++ 无法放置包含常量成员的类的\u back实例

C++ 无法放置包含常量成员的类的\u back实例,c++,c++11,vector,default-constructor,emplace,C++,C++11,Vector,Default Constructor,Emplace,在具有常量成员的类的实例上使用“emplace_back”时遇到问题 请参见下面的示例代码清单 #include <vector> using std::vector; class Data { public: Data(int a, int b, int c) : a(a), b(b), c(c) {} // Removing any/all of these constructors changes nothing! Data& opera

在具有常量成员的类的实例上使用“emplace_back”时遇到问题

请参见下面的示例代码清单

#include <vector>
using std::vector;

class Data {
  public:
    Data(int a, int b, int c) : a(a), b(b), c(c) {}

    // Removing any/all of these constructors changes nothing!
    Data& operator=(const Data& s) = default;
    Data(const Data& s) = default;
    Data(Data&& s) = default;
    ~Data() = default;

    const int a;
    const int b;
    const int c;
};


int main() {
  vector<Data> v;
  Data e(1,2,3);

  // Problem line
  v.emplace_back(4,5,6);
}
我尝试定义赋值运算符(以及移动和复制构造函数以及析构函数),以确保隐式生成的构造函数不会以某种方式变为私有。这并没有改变任何事情。我不明白在这种情况下为什么赋值运算符被“显式删除”


请注意,将类成员更改为非常量不会导致编译错误。

您的clang版本是什么,您的代码可以使用and


当然,默认的
操作符=
是不可能的,因为它无法更新常量成员,但是vector只需要
MoveConstructible
/
CopyConstructible
,不使用
操作符=
,并且您的代码是合法的。

您的clang版本是什么,您的代码可以使用和


当然,默认的
操作符=
是不可能的,因为它无法更新常量成员,但是vector只需要
可移动的
/
可复制的
,并且不使用
操作符=
,并且您的代码是合法的。

注意:常量(或引用)成员不能更改,这使得编写复制/移动赋值或移动构造函数变得困难,包括编译器生成的构造函数。是的,这是有意义的。但是,我不明白为什么对于具有常量成员的类不允许使用复制构造函数?请参阅中间代码< Acc:9:11中的错误消息:注意:候选函数已被明确删除:数据和运算符=(const DATA和S)=默认值;<代码>注意:const(或引用)成员不能更改,因此很难编写复制/移动赋值或移动构造函数,包括编译器生成的构造函数。是的,这很有意义。但是,我不明白为什么对于具有常量成员的类不允许使用复制构造函数?请参阅中间代码< Acc:9:11中的错误消息:注意:候选函数已被明确删除:数据和运算符=(const DATA和S)=默认值;<代码>我下载了Ubuntu clang 3.0-6ubuntu3I版本的clang 3.4二进制文件,我收到了相同的错误消息(有或没有构造函数/运算符定义)。clang只是一个编译器,如果它过时了,你的标准库(libc++或libstdc++)和gcc(用于ld)可能也会更新。我将gcc更新为4.8版,并安装了libstdc++6。这似乎并没有影响到任何东西,也必须更新g++(当然!)。现在可以正常工作了,谢谢:)我下载了3.0-6ubuntu3I版本的Ubuntu clang 3.4二进制文件,我收到了相同的错误消息(有或没有构造函数/运算符定义)。clang只是一个编译器,如果它过时了,你的标准库(libc++或libstdc++)和gcc(用于ld)可能也会更新。我将gcc更新为4.8版,并安装了libstdc++6。这似乎并没有影响到任何东西,也必须更新g++(当然!)。现在一切正常,谢谢:)
% clang++ a.cc  -std=c++11

In file included from a.cc:1:
In file included from /usr/include/c++/4.6/vector:69:
/usr/include/c++/4.6/bits/vector.tcc:319:16: error: overload resolution selected
      deleted operator '='
          *__position = _Tp(std::forward<_Args>(__args)...);
          ~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/vector.tcc:102:4: note: in instantiation of function
      template specialization 'std::vector<Data, std::allocator<Data>
      >::_M_insert_aux<int, int, int>' requested here
          _M_insert_aux(end(), std::forward<_Args>(__args)...);
          ^
a.cc:25:5: note: in instantiation of function template specialization
      'std::vector<Data, std::allocator<Data> >::emplace_back<int, int, int>'
      requested here
  v.emplace_back(4,5,6);
    ^
a.cc:9:11: note: candidate function has been explicitly deleted
    Data& operator=(const Data& s) = default;
          ^
In file included from a.cc:1:
In file included from /usr/include/c++/4.6/vector:60:
/usr/include/c++/4.6/bits/stl_algobase.h:546:18: error: overload resolution
      selected deleted operator '='
            *--__result = std::move(*--__last);
            ~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/bits/stl_algobase.h:578:14: note: in instantiation of
      function template specialization 'std::__copy_move_backward<true, false,
      std::random_access_iterator_tag>::__copy_move_b<Data *, Data *>' requested
      here
      return std::__copy_move_backward<_IsMove, __simple,
             ^
/usr/include/c++/4.6/bits/stl_algobase.h:588:19: note: in instantiation of
      function template specialization 'std::__copy_move_backward_a<true, Data
      *, Data *>' requested here
      return _BI2(std::__copy_move_backward_a<_IsMove>
                  ^
/usr/include/c++/4.6/bits/stl_algobase.h:659:14: note: in instantiation of
      function template specialization 'std::__copy_move_backward_a2<true, Data
      *, Data *>' requested here
      return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
             ^
/usr/include/c++/4.6/bits/vector.tcc:313:4: note: in instantiation of function
      template specialization 'std::move_backward<Data *, Data *>' requested
      here
          _GLIBCXX_MOVE_BACKWARD3(__position.base(),
          ^
/usr/include/c++/4.6/bits/stl_algobase.h:664:48: note: expanded from:
#define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
                                               ^
/usr/include/c++/4.6/bits/vector.tcc:102:4: note: in instantiation of function
      template specialization 'std::vector<Data, std::allocator<Data>
      >::_M_insert_aux<int, int, int>' requested here
          _M_insert_aux(end(), std::forward<_Args>(__args)...);
          ^
a.cc:25:5: note: in instantiation of function template specialization
      'std::vector<Data, std::allocator<Data> >::emplace_back<int, int, int>'
      requested here
  v.emplace_back(4,5,6);
    ^
a.cc:9:11: note: candidate function has been explicitly deleted
    Data& operator=(const Data& s) = default;
          ^
2 errors generated.
a.cc:9:11: note: candidate function has been explicitly deleted
        Data& operator=(const Data& s) = default;