【问题标题】:Compiler reports 'deleted' operator = , but it is there编译器报告“已删除”运算符 = ,但它在那里
【发布时间】:2018-12-28 07:50:28
【问题描述】:

我遇到了一个令人讨厌的问题,编译器声称 operator= 已被删除,但它确实存在。经过几个小时的尝试,我制作了一个重现该问题的最小解决方案。我正在使用 MSVC Community Edition 2017 15.7.5(截至今天的最新版本,2018-07-20),并将其设置为 'C++17'

代码不是微不足道的;这个概念是模板类TT用于强制在一组类Fn中存在一个静态成员函数foo。这与工厂模式非常相似,只是这种解决方案不创建类实例,而是报告类的静态详细信息。

在最后一行的赋值上报错,内容如下(完整的错误列表在底部):

“错误 C2280: 'C &C::operator =(const C &)': 试图引用一个 删除函数”

但是第 5 行定义了这个操作符,对那些装饰器?

失败的赋值尝试将返回的const std::vector<C>& 赋值给类成员变量。
我认为const 产生了问题,但删除每个函数中的所有const 并没有任何区别;同一行中的相同错误。

问题:为什么编译器会报告此问题,有什么可能的解决方法?
我想这一定是我想念的愚蠢的东西,但我找不到它。

#include <vector>

class C
{
public:
  C(int ii) : i(ii) {}
  C& operator=(const C&) = default;    /// HERE is the assignment operator
  const int i;
};
typedef std::vector<C> CVec;   // shorthand for a vector of C's

template <class T>   // this template forces classes F1, F2, ... to have a static member function 'foo'
class TT {
public:
  static const CVec& foo(void) { return T::foo(); }
};

class F1  // one of many Fn classes
{
public:
  static const CVec& foo(void) { static CVec cv{ C{ 1 }, C{ 2 } }; return cv; }    // static member as forced by template
  //...
};
class F2  // another one of many Fn classes
{
public:
  static const CVec& foo(void) { static CVec cv{ C{ 3 } }; return cv; }     // static member as forced by template
  //...
};

class D    // controller class
{
public:
  CVec cv;
  const CVec& bar(int z)   // function to select one of the subclasses
  {
    switch (z)
    {
      case 1: return TT<F1>::foo();
      case 2: return TT<F2>::foo();
        //...
    }
  }

  void foobar(void)  //selector (from user input)
  {
    int z = 2; // user input
    cv = bar(z);   // THIS assignment produces the error
  }
};

完整的错误文本:

------ Build started: Project: BG, Configuration: Debug Win32 ------
bgcore.cpp
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(2443): error C2280: 'C &C::operator =(const C &)': attempting to reference a deleted function
d:\projects\bg\core\bgcore.h(8): note: see declaration of 'C::operator ='
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(2462): note: see reference to function template instantiation '_OutIt std::_Copy_unchecked1<_InIt,_OutIt>(_InIt,_InIt,_OutIt,std::_General_ptr_iterator_tag)' being compiled
        with
        [
            _OutIt=C *,
            _InIt=C *
        ]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1430): note: see reference to function template instantiation '_OutIt *std::_Copy_unchecked<_Iter,C*>(_InIt,_InIt,_OutIt)' being compiled
        with
        [
            _OutIt=C *,
            _Iter=C *,
            _InIt=C *
        ]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1448): note: see reference to function template instantiation 'void std::vector<C,std::allocator<_Ty>>::_Assign_range<_Iter>(_Iter,_Iter,std::forward_iterator_tag)' being compiled
        with
        [
            _Ty=C,
            _Iter=C *
        ]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1448): note: see reference to function template instantiation 'void std::vector<C,std::allocator<_Ty>>::_Assign_range<_Iter>(_Iter,_Iter,std::forward_iterator_tag)' being compiled
        with
        [
            _Ty=C,
            _Iter=C *
        ]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1471): note: see reference to function template instantiation 'void std::vector<C,std::allocator<_Ty>>::assign<C*,void>(_Iter,_Iter)' being compiled
        with
        [
            _Ty=C,
            _Iter=C *
        ]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1471): note: see reference to function template instantiation 'void std::vector<C,std::allocator<_Ty>>::assign<C*,void>(_Iter,_Iter)' being compiled
        with
        [
            _Ty=C,
            _Iter=C *
        ]
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector(1457): note: while compiling class template member function 'std::vector<C,std::allocator<_Ty>> &std::vector<_Ty,std::allocator<_Ty>>::operator =(const std::vector<_Ty,std::allocator<_Ty>> &)'
        with
        [
            _Ty=C
        ]
d:\projects\bg\core\bgcore.h(49): note: see reference to function template instantiation 'std::vector<C,std::allocator<_Ty>> &std::vector<_Ty,std::allocator<_Ty>>::operator =(const std::vector<_Ty,std::allocator<_Ty>> &)' being compiled
        with
        [
            _Ty=C
        ]
d:\projects\bg\core\bgcore.h(22): note: see reference to class template instantiation 'std::vector<C,std::allocator<_Ty>>' being compiled
        with
        [
            _Ty=C
        ]
Done building project "BG.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

【问题讨论】:

  • = default; 可以默认为已删除的版本(就像在这种情况下一样)。您的班级有一个 const 数据成员,因此默认分配无法工作。您可以通过从数据成员中删除 const 来修复它
  • 是的。修复它(让它成为答案)。但是为什么=default 意味着一个已删除的版本???这似乎没有任何意义。如果我删除operator=line,操作符就会被删除,所以如果我添加它,它应该至少告诉我它不能生成一个。
  • 嗯,当你尝试使用它时它告诉你问题......
  • “为什么=default 意味着删除的版本?”因为默认的版本是为了 operator= 被删除,因为该类有一个 const 成员变量。

标签: c++ templates c++17 assignment-operator factory-method


【解决方案1】:

这是您问题的更简短版本:

class C
{
public:
  C(int ii) : i(ii) {}
  C& operator=(const C&) = default;
  const int i;
};

C a(1);
a = a; // error: use of deleted function

虽然您执行了 default 函数,但这并不意味着它一定有效。这只是意味着您明确默认它。默认的复制赋值运算符将一个一个地复制分配所有子对象和成员。但是你的一个成员是const int,你不能复制分配它!这是const

具体规则在[class.copy.assign]/7:

如果 X 具有以下条件,则类 X 的默认复制/移动赋值运算符被定义为已删除: [...] const 非类类型(或其数组)的非静态数据成员,或 [...]

将成员设为int i,就可以了。

【讨论】:

  • 是的,一旦理解了问题,就很明显了。添加更多 Fn 类后,我出现了这些问题;可能只有 F1,编译器优化了它,并且永远不需要 operator=
【解决方案2】:

总是被允许= default 一个特殊的成员函数。这有助于遵守rule of zero or five。它向读者表明您已经考虑过该成员,并且想要默认行为。

【讨论】:

    猜你喜欢
    • 2012-01-13
    • 2014-12-24
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2017-04-29
    • 1970-01-01
    相关资源
    最近更新 更多