【发布时间】: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