【C++0x】 Emulation of scoped enums
scoped enum相当のことは、C++03では以下のように書けるわけですが
struct color { enum enum_t { green, yellow, red }; }; const color::enum_t c = color::green; switch (c) { case color::green: case color::yellow: case color::red: break; }
Boostのライブラリとしてscoped enumの型を提供する際には、C++0xであればenum classを使用し、C++03であればそれをエミュレーションした型を提供する、ということがしたい場合に、ユーザーコードの「::enum_t」が邪魔です。
Boostのdetail以下に入っているBOOST_SCOPED_ENUMなら一応、C++03とC++0xで一様な構文を提供することができますが、ユーザーコードにもマクロが出てきてしまうのでよろしくない。
じゃあどうするか、というとこんな感じに書くことになります:
struct color { enum type { green, yellow, red }; color() : value_() {} color(type value) : value_(value) {} bool operator==(const type& other) const { return value_ == other; } bool operator!=(const type& other) const { return value_ != other; } bool operator==(const color& other) const { return value_ == other.value_; } bool operator!=(const color& other) const { return value_ != other.value_; } operator type() const { return value_; } private: type value_; }; int main() { const color c = color::green; if (c == color::green) {} switch (c) { case color::green: case color::yellow: case color::red: break; } const color yellow = color::yellow; if (c == yellow) {} }
めんどくさいので、おそらくライブラリコードはマクロになるでしょう。