添加按位操作和转换为bool到范围内的枚举 - 一个Christmastide探索

Lig*_*ica 10 c++ enum-class c++17

让我们说我疯了,并决定创造以下怪物:

#include <type_traits>
#include <iostream>

// Utility proxy type - convertible back to E but also permits bool conversion
// for use in conditions.
// 
//  e.g.
//   Foo f = Foo::Bar & Foo::Baz;
//   if (f & Foo::Baz) { /* ... */ }
// 
template <typename E, typename = std::enable_if_t<std::is_enum_v<E>, void>>
struct EnumToBoolProxy
{   
    operator E() const
    {
        return _val;
    }

    explicit operator bool()
    {
        using UT = std::underlying_type_t<E>;
        return static_cast<UT>(_val) != 0;
    }

private:
    const E _val;

    EnumToBoolProxy(const E val) : _val(val) {}

    friend EnumToBoolProxy operator&(const E, const E);
    friend EnumToBoolProxy operator|(const E, const E);
};


enum class Foo
{
    Bar = 1, Baz = 2, Boi = 4
};

EnumToBoolProxy<Foo> operator&(const Foo lhs, const Foo rhs)
{
    using UT = std::underlying_type_t<Foo>;
    return static_cast<Foo>(static_cast<UT>(lhs) & static_cast<UT>(rhs));
}

EnumToBoolProxy<Foo> operator|(const Foo lhs, const Foo rhs)
{
    using UT = std::underlying_type_t<Foo>;
    return static_cast<Foo>(static_cast<UT>(lhs) | static_cast<UT>(rhs));
}


int main()
{
    // Good
    if ((Foo::Bar | Foo::Baz) & Foo::Baz)
        std::cout << "Yay\n";

    // Fine
    const bool isFlagSet((Foo::Bar | Foo::Baz) & Foo::Baz);
    std::cout << isFlagSet << '\n';

    // Meh
    auto proxyThing = (Foo::Bar | Foo::Baz) & Foo::Baz;
}
Run Code Online (Sandbox Code Playgroud)

目标是:

  1. 有一个范围的枚举,使得值是Foo::x和类型Foo(对称!)
  2. 能够对它们进行一些"按位"算术并得到Foo回报
  3. 能够检查结果是否为零
  4. 但是不要让人们通常使用枚举作为算术类型

为了好玩,我试图避免:

  1. 使用沼泽标准枚举
  2. 使用免费功能代替此操作 IsFlagSet

暂时忽略了在没有事先&|操作的情况下无法进行零关系检查的不协调性......

我的用户仍然可以"获得" EnumToBoolProxy(即proxyThing),这似乎是一种耻辱.但是,由于不可能添加任何成员Foo,并且因为operator bool必须是成员,我似乎无法找到任何其他方式来解决这个问题.

认为这不是一个真正的问题,因为他们不能做太多的事情EnumToBoolProxy.但它仍然感觉像一个抽象泄漏,所以我很好奇:我说得对,这本来就不可能吗?没有办法"挑选"像这样的scoped-enum的不透明度?或者有什么方法可以隐藏这种代理类型,同时仍然使用转换为bool工具来检查&/ |operations 的"结果" ?你会怎么做?

flo*_*tan 5

那么这可能不是你想要的,但你说"隐藏这个代理类型".所以你可以把它隐藏在下面甚至更多的怪物中.现在结果类型是一个隐藏你的代理的lambda :)

#include <type_traits>
#include <iostream>

// Utility proxy type - convertible back to E but also permits bool conversion
// for use in conditions.
//
//  e.g.
//   Foo f = Foo::Bar & Foo::Baz;
//   if (f & Foo::Baz) { /* ... */ }
//
auto lam = [](auto e) {

    struct Key {};    

    //template <typename E, typename = std::enable_if_t<std::is_enum_v<E>, void>>
    struct EnumToBoolProxy {
        using E = decltype(e);

        operator E() const {
            return _val;
        }

        explicit operator bool() {
            using UT = std::underlying_type_t<E>;
            return static_cast<UT>(_val) != 0;
        }

        EnumToBoolProxy(const E val, Key) : _val(val) {}


    private:
        const E _val;
    };

    return EnumToBoolProxy(e, Key{});

};

enum class Foo {
    Bar = 1, Baz = 2, Boi = 4
};

auto operator&(const Foo lhs, const Foo rhs) {
    using UT = std::underlying_type_t<Foo>;
    return lam(static_cast<Foo>(static_cast<UT>(lhs) & static_cast<UT>(rhs)));
}

template<typename T, std::enable_if_t<std::is_same_v<T, decltype(lam)>>>
auto operator&(T lhs, const Foo rhs) {
    using UT = std::underlying_type_t<Foo>;
    return lam(static_cast<Foo>(static_cast<UT>(lhs) & static_cast<UT>(rhs)));
}


auto operator|(const Foo lhs, const Foo rhs) {
    using UT = std::underlying_type_t<Foo>;
    return lam(static_cast<Foo>(static_cast<UT>(lhs) | static_cast<UT>(rhs)));
}



int main() {
    lam(Foo::Bar);
    // Good
    if ((Foo::Bar | Foo::Baz) & Foo::Baz)
        std::cout << "Yay\n";

    // Fine
    const bool isFlagSet((Foo::Bar | Foo::Baz) & Foo::Baz);
    std::cout << isFlagSet << '\n';

    // OK, still a proxy thing
    auto proxyThing = (Foo::Bar | Foo::Baz) & Foo::Baz;

    using Proxy = decltype(proxyThing);

    //Proxy proxy2(Foo::Bar); // Does not work anymore.

}
Run Code Online (Sandbox Code Playgroud)


flo*_*tan 5

所以这是另一个解决方案,也许是更严重的解决方案.

它符合您的所有要求,甚至"避免使用沼泽标准枚举".除了Foo值的类型不是Foo,而是CRTP-Foo-ish之类的东西.

User-API类似于真正的枚举,但与其他答案相比具有一些优势: - 不需要贪婪或受SFINAE保护的运算符. - 不再代理课程了.- 是的constexpr.- 零检查可以直接完成,无需打电话&|之前.

#include <type_traits>
#include <iostream>

// Utility proxy type - convertible back to E but also permits bool conversion
// for use in conditions.
//
//  e.g.
//   Foo f = Foo::Bar & Foo::Baz;
//   if (f & Foo::Baz) { /* ... */ }
//

template<unsigned x, typename Base>
struct EnumVal :  std::integral_constant<unsigned, x> {   
};

struct Foo;

template<unsigned x>
using FooVal = EnumVal<x, Foo>;

struct Foo {

    static constexpr FooVal<1> Bar;
    static constexpr FooVal<2> Baz;
    static constexpr FooVal<4> Boi;
};


template<unsigned lhs, unsigned rhs>
EnumVal<(lhs & rhs), Foo> constexpr operator&( EnumVal<lhs, Foo> ,  EnumVal<rhs, Foo> ) {
    return {};
}


template<unsigned lhs, unsigned rhs>
EnumVal<(lhs | rhs), Foo> constexpr operator|( EnumVal<lhs, Foo> ,  EnumVal<rhs, Foo> ) {
    return {};
}


template<typename T>
constexpr void print_type(T) {
    static_assert(std::is_same_v<T, void>, "YOU WANTED TO READ THIS TYPE!");
}

int main() {
   // Not an arithmetic type :)
   static_assert(!std::is_arithmetic_v<decltype(Foo::Bar)>);

    static_assert(Foo::Bar);
    static_assert(!(Foo::Bar & Foo::Baz));

    // Good
    if ((Foo::Bar | Foo::Baz) & Foo::Baz)
        std::cout << "Yay\n";

    // Fine
    const bool isFlagSet = (Foo::Bar | Foo::Baz) & Foo::Baz;
    std::cout << isFlagSet << '\n';

    // Finally really not a proxy thing anymore!
    auto proxyThing = (Foo::Bar | Foo::Baz) & Foo::Baz;
    // print_type(proxyThing);

}
Run Code Online (Sandbox Code Playgroud)