梅花雀莊

何震邦的個人網站,通常寫電腦和橋牌相關的文章,未來可能會寫關於日本麻將的東西

標籤:Cxx

本標籤的正確名稱應該是 C++,但因為技術限制只好改為 Cxx。

在 C++98 模擬 enum class

使用巢狀類別 (nested class) 可以達到類似的效果。因為我們不需要這些類別的實例 (instance),所以只需要宣告 (declaration),不需要定義 (definition)。

struct Color
{
    class Red;
    class Green;
    class Blue;
};

template<typename ColorType>
struct hex;

template<>
struct hex<Color::Red>
{ enum { value = 0xff0000 }; };

template<>
struct hex<Color::Green>
{ enum { value = 0x00ff00 }; };

template<>
struct hex<Color::Blue>
{ enum { value = 0x0000ff }; };