What is the appropriate pattern for using enumerations as flags in modern C++?
The question stems from my reading of the technical specification A Proposal to Add 2D Graphics Rendering and Display to C++, where McLaughlin, Sutter, and Zink propose a C++ API for 2D graphics based on the Cairo API.
Throughout the API declaration, the authors make full use of C++11. In particular, their enumerations are all declared like:
enum class font_slant {
normal,
italic,
oblique
};
Except for a single one:
namespace text_cluster_flags {
enum text_cluster_flags : int {
none = 0x0,
backward = 0x1
};
}
The text_cluster_flags
"type" is used in a class method:
void show_text_glyphs(const ::std::string& utf8,
const ::std::vector<glyph>& glyphs,
const ::std::vector<text_cluster>& clusters,
text_cluster_flags cluster_flags);
I assume the extraneous declaration is so that the text_cluster_flags
can be masked, as in:
auto flag = text_cluster_flags::none | text_cluster_flags::backward;
which you cannot do with enum class
enumerations:
enum class flags {
a = 0x0,
b = 0x1
};
auto f = flags::a | flags::b; // error because operator `|` is
// not defined for enum class flags
// values
Should the authors have defined the masking operators? Or is their enum-within-namespace pattern effective practice?