Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Enum in C++

📅 2015-Jan-20 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, enum ⬩ 📚 Archive

C++11 introduced a much better enumeration. It provides a named scope for its enumerators and is strongly typed:

enum class FileType
{
    Png,
    Jpg,
    Bmp
};

write_file(img_data, "foo", FileType::Jpg);

When using an older C++ compiler, I try to get the scope feature of C++11 by using a namespace:

namespace FileType
{
    enum Enum
    {
        Png,
        Jpg,
        Bmp
    };
}

// Excepts the type FileType::Enum
write_file(img_data, "foo", FileType::Jpg);

© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 Mastodon📧 Email