我注意到C++不会编译以下内容:
class No_Good {
static double const d = 1.0;
};
Run Code Online (Sandbox Code Playgroud)
但是,它会愉快地允许将double更改为int,unsigned或任何整数类型的变体:
class Happy_Times {
static unsigned const u = 1;
};
Run Code Online (Sandbox Code Playgroud)
我的解决方案是将其改为:
class Now_Good {
static double d() { return 1.0; }
};
Run Code Online (Sandbox Code Playgroud)
并认为编译器将足够智能以在必要时内联...但它让我很好奇.
为什么C++设计器允许我使用静态const一个int或unsigned,而不是一个double?
编辑:我在Windows XP上使用visual studio 7.1(.net 2003).
EDIT2:
问题已得到解答,但为了完成,我看到的错误:
error C2864: 'd' : only const static integral data members can be initialized inside a class or struct
Run Code Online (Sandbox Code Playgroud) c++ ×1