Использование BOOST_STRONG_TYPEDEF для различения типов arg, но вызывающее ошибку seg
У меня есть метод, требующий нескольких переменных одного типа перечисления. Чтобы компилятор мог определить, передаю ли я неправильный аргумент, который я использую BOOST_STRONG_TYPEDEF
. Однако я получаю ошибку сегмента, когда создаю экземпляр и сравниваю его в операторе IF.
Версия Boost 1.74
enum class Testable
{
UNDEFINED,
A,
B
};
BOOST_STRONG_TYPEDEF(Testable, SomeType)
int main()
{
SomeType abc{Testable::UNDEFINED};
std::cout << "START" << std::endl;
if(abc == Testable::UNDEFINED) // Seg faults here
{
volatile int j = 0;
}
std::cout << "FINISH" << std::endl;
}
Трассировка GDB предполагает, что это вызов переполнения стека / рекурсивный вызов:
#1 0x00007ffff74c5d9d in boost::operators_impl::operator== (y=@0x7fffffcc9e44:
#2 0x00007ffff74c5d9d in boost::operators_impl::operator== (y=@0x7fffffcc9e44:
#3 0x00007ffff74c5d9d in boost::operators_impl::operator== (y=@0x7fffffcc9e44:
#4 0x00007ffff74c5d9d in boost::operators_impl::operator== (y=@0x7fffffcc9e44:
#5 0x00007ffff74c5d9d in boost::operators_impl::operator== (y=@0x7fffffcc9e44:
#6 0x00007ffff74c5d9d in boost::operators_impl::operator== (y=@0x7fffffcc9e44:
Документации для BOOST_STRONG_TYPEDEF
. Я неправильно это использую?
Версия Boost - 1.74. Я использую Clang.
Ответы
Дезинфицирующее средство говорит
==3044==ERROR: AddressSanitizer: stack-overflow on address 0x7ffcc58b3ff8 (pc 0x56310c340e84 bp 0x7ffcc58b4000 sp 0x7ffcc58b3ff
0 T0)
#0 0x56310c340e84 in boost::operators_impl::operator==(Testable const&, SomeType const&) /home/sehe/custom/boost_1_75_0/boo
Проблема в том, что STRONG_TYPEDEF Boost делает производный тип полностью упорядоченным с базовым типом:
struct SomeType
: boost::totally_ordered1<SomeType, boost::totally_ordered2<SomeType, Testable>>
{
Testable t;
explicit SomeType(const Testable& t_) noexcept((boost::has_nothrow_copy_constructor<Testable>::value)) : t(t_) {}
SomeType() noexcept( (boost::has_nothrow_default_constructor<Testable>::value)) : t() {}
SomeType(const SomeType& t_) noexcept( (boost::has_nothrow_copy_constructor<Testable>::value)) : t(t_.t) {}
SomeType& operator=(const SomeType& rhs) noexcept((boost::has_nothrow_assign<Testable>::value)) {
t = rhs.t;
return *this;
}
SomeType& operator=(const Testable& rhs) noexcept((boost::has_nothrow_assign<Testable>::value)) {
t = rhs;
return *this;
}
operator const Testable&() const { return t; }
operator Testable&() { return t; }
bool operator==(const SomeType& rhs) const { return t == rhs.t; }
bool operator<(const SomeType& rhs) const { return t < rhs.t; }
};
Если вы удалите этот источник неявного преобразования:
struct SomeType
: boost::totally_ordered1<SomeType
/*, boost::totally_ordered2<SomeType, Testable>*/>
{
// ...
это JustWorks ™. Я бы сказал, что вы также должны сделать операторы преобразования explicit
и всегда выполнять приведение:
Live On Coliru
#include <boost/serialization/strong_typedef.hpp>
#include <iostream>
enum class Testable { UNDEFINED, A, B };
struct SomeType
: boost::totally_ordered1<SomeType
/*, boost::totally_ordered2<SomeType, Testable>*/>
{
Testable t;
explicit SomeType(const Testable& t_) noexcept((boost::has_nothrow_copy_constructor<Testable>::value)) : t(t_) {}
SomeType() noexcept( (boost::has_nothrow_default_constructor<Testable>::value)) : t() {}
SomeType(const SomeType& t_) noexcept( (boost::has_nothrow_copy_constructor<Testable>::value)) : t(t_.t) {}
SomeType& operator=(const SomeType& rhs) noexcept((boost::has_nothrow_assign<Testable>::value)) {
t = rhs.t;
return *this;
}
SomeType& operator=(const Testable& rhs) noexcept((boost::has_nothrow_assign<Testable>::value)) {
t = rhs;
return *this;
}
explicit operator const Testable&() const { return t; }
explicit operator Testable&() { return t; }
bool operator==(const SomeType& rhs) const { return t == rhs.t; }
bool operator<(const SomeType& rhs) const { return t < rhs.t; }
};
int main() {
SomeType abc{ Testable::UNDEFINED };
std::cout << "START" << std::endl;
if (abc == SomeType{Testable::UNDEFINED}) {
volatile int j = 0;
}
std::cout << "FINISH" << std::endl;
}