我不断收到以下错误消息,告诉我我正在使用一个已删除的函数,我认为它是std::variant默认构造函数。
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;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
代码大致为:
#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(的初始值设定项列表中添加一个默认构造函数调用,但似乎也不起作用。感谢您对理解这条神秘信息的任何帮助。抱歉代码太长。
虽然您确实需要处理一个最小的示例,但核心问题是您的DraftState默认构造函数与带有默认参数的字符串构造函数不明确。看见https://godbolt.org/z/hTnsjoWaW
std::variant
要求第一个类型参数是默认可构造的。这种模糊性导致编译器认为您的类不是默认可构造的,因此变体也不是。
此外,Document的移动构造函数应该使用成员初始值设定项列表,而不是赋值。并且您的DraftState缺少复制赋值运算符,除非有更多的运算符,否则我不会显式定义所有的复制/移动/析构函数值。参见五条规则。