Я пытаюсь создать подкласс std:: optional в MS C++ 17 (VS2017), чтобы добавить поле сообщения в класс, но получаю ошибку компиляции
ошибка C2280: '
OptMsg<bool>::OptMsg(const OptMsg<bool> &)
': попытка сослаться на удаленную функцию
Intellisense дает немного больше информации:
на функцию "
OptMsg<T>::OptMsg(const OptMsg<bool> &) throw() [with T=bool]
" (объявленную неявно) нельзя ссылаться - это удаленная функция
Что говорит мне о том, что у компилятора возникла проблема с моим конструктором копирования, ссылающимся на бросок удаленной функции? Я получаю сообщение об ошибке, возвращающее экземпляр из функции. Например.,
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;
};
std::optional
действительно предназначен для наследования. Я думаю, что композиция была бы лучшим выбором дизайна здесь. - person Some programmer dude   schedule 17.07.2019