Вот решение на основе шаблона оболочки.Я думаю, что это далеко от идеала, но он должен работать как ожидалось.
template <class T>
struct PrintableOpt {
std::optional<T> value;
};
Необходимые перегрузки операторов будут
template <class T>
std::ostream& operator << (std::ostream& os, const PrintableOpt<T>& opt)
{
if (opt.value)
return os << *opt.value;
else
return os << "std::nullopt";
}
template <class T, class U>
bool operator == (const PrintableOpt<T>& lhs, const PrintableOpt<U>& rhs)
{
return lhs.value && rhs.value && *lhs.value == *rhs.value;
}
template <class T, class U>
bool operator != (const PrintableOpt<T>& lhs, const PrintableOpt<U>& rhs)
{
return !(lhs == rhs);
}
, и для удобства, вот две вспомогательные функции для построенияЭкземпляры-оболочки:
template <class T>
auto printable(T&& opt)
{
return PrintableOpt<T>{std::forward<T>(opt)};
}
template <class T>
auto printable(std::optional<T>& opt)
{
return PrintableOpt<T>{opt};
}
Теперь тест будет выглядеть так:
BOOST_AUTO_TEST_CASE(test1)
{
std::optional<int> opt1(10);
BOOST_TEST(printable(opt1) == printable(11));
std::optional<long> opt2(11);
BOOST_CHECK_EQUAL(printable(opt1), printable(opt2));
}