首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C++编译器错误:使用已删除的函数std::variant()

C++编译器错误:使用已删除的函数std::variant()
EN

Stack Overflow用户
提问于 2021-11-17 21:00:13
回答 1查看 148关注 0票数 0

我不断地收到以下错误消息,告诉我正在使用一个deleted函数,我认为它是std::variant默认构造函数。

代码语言:javascript
运行
复制
In file included from main.cpp:2:
Document.hpp: In instantiation of ‘Document<StateVariant, EventVariant, Transitions>::Document(StateVariant&&) [with StateVariant = std::variant<DraftState, PublishState>; EventVariant = std::variant<EventWrite, EventRead>; Transitions = TransitionRegister]’:
main.cpp:7:61:   required from here
Document.hpp:33:37: error: use of deleted function ‘std::variant<_Types>::variant() [with _Types = {DraftState, PublishState}]’
   33 |    Document(StateVariant &&a_state) {
      |                                     ^
In file included from Document.hpp:6,
                 from main.cpp:2:
/usr/include/c++/11/variant:1385:7: note: ‘std::variant<_Types>::variant() [with _Types = {DraftState, PublishState}]’ is implicitly deleted because the default definition would be ill-formed:
 1385 |       variant() = default;
      |       ^~~~~~~
/usr/include/c++/11/variant:1385:7: error: use of deleted function ‘constexpr std::_Enable_default_constructor<false, _Tag>::_Enable_default_constructor() [with _Tag = std::variant<DraftState, PublishState>]’
In file included from /usr/include/c++/11/variant:38,
                 from Document.hpp:6,
                 from main.cpp:2:
/usr/include/c++/11/bits/enable_special_members.h:112:15: note: declared here
  112 |     constexpr _Enable_default_constructor() noexcept = delete;
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~

代码大致如下:

代码语言:javascript
运行
复制
#include <iostream>
#include <variant>
class DraftState {
public:
   DraftState() = default; // default constructor
   DraftState(const DraftState &a_state) { m_msg = a_state.m_msg; } // copy constructor
   DraftState(const std::string &a_rMsg = "") { m_msg = a_rMsg; } // custom constructor
   DraftState(DraftState &&a_state) { m_msg = std::move(a_state.m_msg);} // move constructor
   DraftState& operator=(DraftState &&a_state) { if(this != &a_state) { m_msg = std::move(a_state.m_msg); } return *this; } // move assignable constructor

   ~DraftState() = default; // destructor
   std::string getState() { return "DraftState"; }
   std::string m_msg;
};

class PublishState{
   // similar to DraftState
};

using State = std::variant<DraftState, PublishState>;

template<typename StateVariant>
class Document
{
public:
   Document() = default;
   Document(StateVariant &&a_state) {
      m_state = std::move(a_state);
   }
   StateVariant m_state;
//...
};

int main()
{
   DraftState draftState("draft");
   Document<State> doc(draftState);
   return 0;
}

我已经尝试在自定义构造函数文档(StateVariant&&a_state)的初始化器列表中添加默认构造函数调用,但似乎也不起作用。如果能帮助理解这条隐秘的消息,我将不胜感激。抱歉,代码太长了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-17 21:25:40

虽然您确实需要处理一个最小的示例,但核心问题是您的DraftState默认构造函数与带有默认参数的字符串构造函数不明确。请参阅https://godbolt.org/z/hTnsjoWaW

要成为默认可构造的,std::variant要求第一个类型参数是默认的可构造的。歧义导致编译器认为您的类不是默认可构造的,因此变体也不是。

此外,文档的移动构造函数应该使用成员初始值设定项列表,而不是赋值。而且您的DraftState缺少复制赋值运算符,但是除非有更多的赋值运算符,否则我不会显式定义所有的复制/移动/析构函数值。请参阅Rule of Five

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70011477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档