【发布时间】:2018-09-11 22:35:51
【问题描述】:
我有一个包含其他类型的模板类属性:
template <typename T>
class Property
{
private:
T value;
public:
Property() = default;
Property(const T& initialValue)
: value(initialValue) {}
virtual ~Property() = default;
//Make this class non-copyable.
Property(const Property&) = delete;
Property& operator=(const Property&) = delete;
virtual Property& operator=(const T& other)
{
value = other;
return *this;
}
//... a bunch of other unimportant stuff
}
Visual Studio 15.7.6 和其他一些编译器非常满意
{ //function or class definition (default member initialization)
Property<int> prop = 5;
}
但是,(针对专有编译目标稍作修改)GCC 4.9.4 在上述声明中失败:
Error GD4849D22 use of deleted function
'Property<T>::Property(const Property<T>&) [with T = int]'
编译器似乎正在尝试构造一个 RValue 属性,然后使用已删除的复制构造函数,而不是简单地使用类型适当的构造函数。
这是 GCC 过于谨慎的例子吗?
Property<int> prop(5); //explicit constructor call - valid with GCC Compiler
Property<int> myOtherProp;
myOtherProp = 5; //Also fine (obviously)
或者是 MSVC 玩得又快又松,做一些标准规定不应该或不必做的事情?
很遗憾,我无法更新我的 GCC 版本。因为存在解决方法,所以我更多地寻找“为什么”会发生这种情况。
【问题讨论】:
-
GCC 4.9.4- 非常过时。 -
@NeilButterworth,正如我在问题中所说,我正在使用专有目标并且无法更改我的 gcc 版本。