【发布时间】:2017-03-29 12:50:58
【问题描述】:
我有几行在我的系统上编译得很好,但在同事系统上却没有编译。这就是为什么我想问一下这个问题的首选解决方案是什么样的。我必须处理一个enum,它隐含地定义了我必须为std::array 提供多少空间。代码的其他部分也利用了FooSize 是静态的。 (优化)
我目前的实现是这样的
enum class FooType
{
ShortFoo,
LongFoo
};
// defined in a different file
template <FooType FType>
class FooContainer
{
public:
static const unsigned int FooSize {(FType == FooType::ShortFoo) ? 32 : 64 };
std::array<float, FooSize> fooArray;
};
该代码似乎在较旧的 llvm / clang 编译器上产生了问题。 32 和 64 实际上是通过预处理器定义提供的。我可以跳过FooType 并将大小用作模板参数,但我想知道初始化FooSize 的最可靠方法是什么。
【问题讨论】:
-
clang 编译器有多老?他们支持 C++11? (枚举类和
std::array是 C++11 引入的特性)。并且,请您转录确切的错误吗? -
LLVM 5.1,所以它是 XCode 5.1.1。我们使用 C++11,这有点奇怪。 (抱怨这个的编译器不会支持 c++11)错误:
in-class initializer for static data member is not a constant expression
标签: c++ c++11 templates static-members static-initialization