2

在现代 C++ 中使用枚举作为标志的适当模式是什么?

这个问题源于我对技术规范A Proposal to Add 2D Graphics Rendering and Display to C++的阅读,其中 McLaughlin、Sutter 和 Zink 提出了基于Cairo API 的用于 2D 图形的 C++ API

在整个 API 声明中,作者充分利用了 C++11。特别是,它们的枚举都被声明为:

enum class font_slant {
  normal,
  italic,
  oblique
};

除了一个:

namespace text_cluster_flags {
  enum text_cluster_flags : int {
    none     = 0x0,
    backward = 0x1
  };
}

text_cluster_flags类型”用于类方法中:

void show_text_glyphs(const ::std::string& utf8,
    const ::std::vector<glyph>& glyphs,
    const ::std::vector<text_cluster>& clusters,
    text_cluster_flags cluster_flags);

我假设无关的声明是text_cluster_flags可以掩盖的,如:

auto flag = text_cluster_flags::none | text_cluster_flags::backward;

你不能用enum class枚举来做:

enum class flags {
  a = 0x0,
  b = 0x1
};

auto f = flags::a | flags::b; // error because operator `|` is
                              // not defined for enum class flags
                              // values

作者是否应该定义屏蔽运算符?或者他们的enum-within-namespace模式是有效的实践吗?

4

1 回答 1

2

它以cairo API为蓝本。

因为font_slant我们可以看到 cairo 的等价物:

枚举 cairo_font_slant_t

typedef enum {
    CAIRO_FONT_SLANT_NORMAL,
    CAIRO_FONT_SLANT_ITALIC,
    CAIRO_FONT_SLANT_OBLIQUE
} cairo_font_slant_t;

根据倾斜度指定字体的变体。

      CAIRO_FONT_SLANT_NORMAL直立字体样式,从 1.0 开始

      CAIRO_FONT_SLANT_ITALIC斜体字体样式,从 1.0 开始

      CAIRO_FONT_SLANT_OBLIQUEOblique 字体样式,从 1.0 开始

因为text_cluster_flags我们可以看到 cairo 的等价物:

枚举 cairo_text_cluster_flags_t

typedef enum {
    CAIRO_TEXT_CLUSTER_FLAG_BACKWARD = 0x00000001
} cairo_text_cluster_flags_t;

指定文本簇映射的属性。

      CAIRO_TEXT_CLUSTER_FLAG_BACKWARD簇数组中的簇从头到尾映射到字形数组中的字形。(从 1.8 开始)

函数text_to_glyphs模型cairo_show_text_glyphs,它需要一个cairo_text_cluster_flags_t. 此外,API 具有获取当前倾斜度的功能。所以我的猜测是:

  • enum class用于标记的强类型。拥有“正常”和“斜体”的东西没有任何意义。这些附加到“字体”。

  • text_cluster_flags是一次性交易。如果您为 show glyphs 功能设置它,它只会改变行为。它不附加到“文本簇”,就像倾斜附加到“字体”一样。这里没有强类型的理由。

顺便说一句,您的解释是正确的。这是源代码的片段:

// ...

+       const cairo_glyph_t *cur_glyph;
+
+       if (cluster_flags & CAIRO_TEXT_CLUSTER_FLAG_BACKWARD)
+       cur_glyph = glyphs + num_glyphs - 1;
+       else
+       cur_glyph = glyphs;
+
+       for (i = 0; i < num_clusters; i++) {
+       cairo_bool_t cluster_visible = FALSE;
+

// ...
于 2015-09-11T21:15:18.577 回答