如何使用枚举类作为一组标志

How does one use an enum class as a set of flags?

本文关键字:一组 标志 何使用 枚举      更新时间:2023-10-16

假设我有一组标志和一个类似的类:

/// <summary>Options controlling a search for files.</summary>
enum class FindFilesOptions : unsigned char
{
    LocalSearch = 0,
    RecursiveSearch = 1,
    IncludeDotDirectories = 2
};
class FindFiles : boost::noncopyable
{
    /* omitted */
public:
    FindFiles(std::wstring const& pattern, FindFilesOptions options);
    /* omitted */
}

我希望来电者能够选择不止一个选项:

FindFiles handle(Append(basicRootPath, L"*"),
    FindFilesOptions::RecursiveSearch | FindFilesOptions::IncludeDotDirectories);

有没有可能用C++11enum class以强类型的方式支持这一点,或者我必须恢复到非类型枚举?

(我知道调用者可以static_cast到底层类型,static_cast返回,但我不希望调用者必须这样做)

位图当然可以使用enum classes。不幸的是,这样做有点痛苦:您需要对您的类型定义必要的位操作。下面是一个示例。如果enum classes可以从其他类型派生,这将是一件很好的事情,这些类型可以位于一个合适的命名空间中,定义必要的运算符样板代码。

#include <iostream>
#include <type_traits>
enum class bitmap: unsigned char
{
    a = 0x01,
    b = 0x02,
    c = 0x04
};
bitmap operator& (bitmap x, bitmap y)
{
    typedef std::underlying_type<bitmap>::type uchar;
    return bitmap(uchar(x) & uchar(y));
}
bitmap operator| (bitmap x, bitmap y)
{
    typedef std::underlying_type<bitmap>::type uchar;
    return bitmap(uchar(x) | uchar(y));
}
bitmap operator^ (bitmap x, bitmap y)
{
    typedef std::underlying_type<bitmap>::type uchar;
    return bitmap(uchar(x) ^ uchar(y));
}
bool test(bitmap x)
{
    return std::underlying_type<bitmap>::type(x);
}
int main()
{
    bitmap v = bitmap::a | bitmap::b;
    if (test(v & bitmap::a)) {
        std::cout << "a ";
    }
    if (test(v & bitmap::b)) {
        std::cout << "b ";
    }
    if (test(v & bitmap::c)) {
        std::cout << "c ";
    }
    std::cout << 'n';
}

模板与enum class配合良好,因此您可以定义在类似枚举类型的集合上工作的运算符集。关键是使用traits模板来指定每个枚举符合/订阅的接口

首先:

enum class mood_flag {
    jumpy,
    happy,
    upset,
    count // size of enumeration
};
template<>
struct enum_traits< mood_flag > {
    static constexpr bool bit_index = true;
};
template< typename t >
struct flag_bits : std::bitset< static_cast< int >( t::count ) > {
    flag_bits( t bit ) // implicit
        { this->set( static_cast< int >( bit ) ); }
    // Should be explicit but I'm lazy to type:
    flag_bits( typename flag_bits::bitset set )
        : flag_bits::bitset( set ) {}
};
template< typename e >
typename std::enable_if< enum_traits< e >::bit_index,
    flag_bits< e > >::type
operator | ( flag_bits< e > set, e next )
    { return set | flag_bits< e >( next ); }
template< typename e >
typename std::enable_if< enum_traits< e >::bit_index,
    flag_bits< e > >::type
operator | ( e first, e next )
    { return flag_bits< e >( first ) | next; }

http://ideone.com/kJ271Z

GCC 4.9报告说,在我编译的时候,一些隐式成员函数是constexpr,所以模板可能也是这样。

它可能还应该有一个自由函数to_scalar或返回一个无符号整数类型的东西,给定一个单独的标志或flag_bits集。

如何定义FindFiles,使其取FindFilesOptionsstd::initializer_list

void FindFiles(std::wstring const& pattern, std::initializer_list<FindFilesOptions> options)
{
  auto has_option = [&](FindFilesOptions const option)
  {
    return std::find(std::begin(options), std::end(options), option) != std::end(options);
  };
  if (has_option(FindFilesOptions::LocalSearch))
  {
    // ...
  }
  if (has_option(FindFilesOptions::RecursiveSearch))
  {
    // ...
  }
  if (has_option(FindFilesOptions::IncludeDotDirectories))
  {
    // ...
  }
}

然后你可以这样称呼它:

FindFiles({}, {FindFilesOptions::RecursiveSearch, FindFilesOptions::IncludeDotDirectories});

问题不在于显式枚举类型,而在于类作用域。

使用C++11,当您需要对值进行操作(按位操作、递增等)时,与一堆constexpr相比,枚举作为编译时常数会引起很多兴趣。

如果您不关心性能,请将选项更改为set<FindFilesOptions>