我正在尝试在 MS C++17(VS2017)中对 std::optional 进行子类化以向类添加消息字段,但出现编译错误
错误 C2280:“
OptMsg<bool>::OptMsg(const OptMsg<bool> &)
”:试图引用已删除的函数
Intellisense 提供了更多的洞察力:
函数“
OptMsg<T>::OptMsg(const OptMsg<bool> &) throw() [with T=bool]
”(隐式声明)不能被引用——它是一个被删除的函数
这告诉我编译器在引用已删除函数 throw 的复制构造函数方面存在问题?我得到错误返回一个函数的实例。例如,
OptMsg<bool> foo()
{
OptMsg<bool> res = false;
return res; // <-- Getting compile error here
}
这是我的课。任何见解表示赞赏!
template <class T>
class KB_MAPPING_ENGINE_API OptMsg : public std::optional<T>
{
public:
constexpr OptMsg() noexcept
: optional{}
{}
constexpr OptMsg(std::nullopt_t) noexcept
: optional{}
{}
constexpr OptMsg(const T & other) noexcept
: optional<T>{other}
, m_Message{other.m_Message}
{}
constexpr explicit OptMsg(const T && other) noexcept
: optional<T>{other}
, m_Message{other.m_Message}
{}
OptMsg & operator = ( const OptMsg & other ) noexcept
{
if ( &other != this )
m_Message = other.m_Message;
return *this;
}
OptMsg && operator = ( const OptMsg && other )
{
if ( &other != this )
m_Message = other.m_Message;
return *this;
}
void SetMessage( const std::string & message ) { m_Message = message; }
std::string GetMessage() { return m_Message; }
private:
std::string m_Message;
};