我有以下示例(扩展为Avoid calling move constructor)
#include <cstdint>
class Interface
{
public:
Interface() = default;
virtual ~Interface() = default;
Interface(const Interface&) = delete;
Interface(Interface&&) = delete;
const Interface& operator=(const Interface&) = delete;
Interface& operator=(Interface&&) = delete;
};
class FooC : public Interface
{
public:
FooC(uint16_t iPort, uint16_t iPin)
: PORT(iPort)
, PIN(iPin)
{
};
FooC() = delete;
~FooC() override = default;
FooC(const FooC&) = delete;
FooC(FooC&&) = delete;
const FooC& operator=(const FooC&) = delete;
FooC& operator=(FooC&&) = delete;
private:
const uint16_t PORT;
const uint16_t PIN;
};
class FactoryC
{
public:
FactoryC()
: mFoo{
{1, 2},
{3, 4}
}
{
};
~FactoryC() = default;
FactoryC(const FactoryC&) = delete;
FactoryC(FactoryC&&) = delete;
const FactoryC& operator=(const FactoryC&) = delete;
FactoryC& operator=(FactoryC&&) = delete;
private:
FooC mFoo[2];
};
int main()
{
FactoryC factory{};
}
,我不想调用默认的,移动和复制构造函数。因此,我删除了功能。不幸的是,这导致以下错误(与C ++ 17一起编译)
<source>: In constructor 'FactoryC::FactoryC()':
<source>:42:4: error: use of deleted function 'FooC::FooC(FooC&&)'
}
^
<source>:26:4: note: declared here
FooC(FooC&&) = delete;
^~~~
Compiler returned: 1
在此示例中是否可以使用参数强制调用构造函数,并且仍然删除FooC的默认,移动和复制构造函数?
答案 0 :(得分:4)
这似乎是一个错误。 @SergeyA的评论:
这肯定看起来像是个错误。使Interface析构函数成为非虚拟的(并相应地删除了覆盖)可以解决编译问题。
建议该问题与虚拟基类有关。确实,bug report #86849处理了一个无关的问题,理查德·史密斯(Richard Smith)得出了以下结论:
有趣的是,如果该类具有虚拟基类,则GCC确实会抑制复制保证。