程序使用 clang++ 编译,但 g++ 耗尽 RAM 并失败

Program compiles using clang++, but g++ exhausts the RAM and fails

我考虑了引入的基于 C++11 的枚举位集 here。我想出了一些示例程序:

#include <bitset>
#include <type_traits>
#include <limits>

template <typename TENUM>
class FlagSet {

private:
  using TUNDER = typename std::underlying_type<TENUM>::type;
  std::bitset<std::numeric_limits<TUNDER>::max()> m_flags;

public:
  FlagSet() = default;

  FlagSet(const FlagSet& other) = default;
};

enum class Test
{
  FIRST,
  SECOND
};


int main(int argc, char *argv[])
{
  FlagSet<Test> testFlags;
  return 0;
}

程序通过 clang++ -std=c++11 -o main main.cc 使用 clang++(clang 版本 3.8.1 (tags/RELEASE_381/final))编译得很好。 但是,如果我通过 g++ -std=c++11 -o main main.cc 使用 g++ (g++ (GCC) 6.2.1 20160830),编译器最终会耗尽系统内存。这是 g++ 的问题还是这段代码不符合标准?

std::bitset<std::numeric_limits<TUNDER>::max()> 大小为 256 MiB(假设 32 位 int)。 clang 成功编译它很棒,但 gcc 内存不足并不奇怪。

如果您打算将枚举器用作位集索引,则必须将最大的枚举器作为单独的模板参数传入;目前 (Max and min values in a C++ enum) 无法找到枚举的范围。

示例:

template <typename TENUM, TENUM MAX>
class FlagSet {

private:
  std::bitset<MAX + 1> m_flags;

public:
  FlagSet() = default;

  FlagSet(const FlagSet& other) = default;
};

enum class Test
{
  FIRST,
  SECOND,
  MAX = SECOND
};

FlagSet<Test, Test::MAX> testFlags;