C++ 使用BOOST_STRONG_TYPEDEF区分arg类型,但导致seg故障

C++ 使用BOOST_STRONG_TYPEDEF区分arg类型,但导致seg故障,c++,boost,C++,Boost,我有一个方法需要几个相同枚举类型的变量。为了让编译器检测我是否传递了错误的参数,我使用了BOOST\u STRONG\u TYPEDEF。但是,当我创建一个实例并在IF语句中进行比较时,我会遇到seg错误 Boost版本是1.74 enum class Testable { UNDEFINED, A, B }; BOOST_STRONG_TYPEDEF(Testable, SomeType) int main() { SomeType abc{Testab

我有一个方法需要几个相同枚举类型的变量。为了让编译器检测我是否传递了错误的参数,我使用了
BOOST\u STRONG\u TYPEDEF
。但是,当我创建一个实例并在IF语句中进行比较时,我会遇到seg错误

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;
}
关于
BOOST\u STRONG\u 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
问题是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; }
};

由于
enum类本身是一个强类型,我不清楚为什么有必要在它上面加上其他一些“强类型定义”。@SamVarshavchik我有一个函数处理该类型的4个实例。它们有上下文差异。为了防止传递错误的实例,我正在使用Boost Strong Type有效地创建一个“子类型”。。。我只需要声明四个
struct
s,其中包含这个
enum类的一个实例
,作为它们的唯一成员,定义一个默认的
==
=运算符和吞并枚举类的构造函数(可能不需要,这要感谢C++中的聚合初始化)。好消息是:您可以显式地
所有构造函数和转换运算符。糟糕的新情况是,它确实看起来像lile自c++20以来,行为发生了变化,导致“错误编译”(这是正确的代码生成,但不同于c++17行为):这可能会报告给Boost操作符devs,将其简化为一个新问题并问哦,删除
totally_ordered2
基类仍然可以解决问题-以防您不确定。原则上,我觉得这个问题很有趣,但这不应该让你太担心:)相关的现有图书馆问题:,也有很多问题context@user997112我可以使用
显式
转换运算符
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>*/>
{
     // ...
#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;
}