我有一个方法需要相同枚举类型的多个变量。为了让编译器检测我是否传递了错误的参数,我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)
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:
Run Code Online (Sandbox Code Playgroud)
没有太多的文档BOOST_STRONG_TYPEDEF。难道是我用错了?
Boost版本是1.74。我正在使用铿锵。
消毒剂说
==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
Run Code Online (Sandbox Code Playgroud)
问题在于 Boost 的 STRONG_TYPEDEF 使派生类型与基类型全序:
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; }
};
Run Code Online (Sandbox Code Playgroud)
如果删除该隐式转换源:
struct SomeType
: boost::totally_ordered1<SomeType
/*, boost::totally_ordered2<SomeType, Testable>*/>
{
// ...
Run Code Online (Sandbox Code Playgroud)
它JustWorks(TM)。我认为你也应该使用转换运算explicit符并始终进行强制转换:
#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;
}
Run Code Online (Sandbox Code Playgroud)