我有一个类,它基本上只包含我的应用程序使用的一堆常量定义.但出于某种原因,longs编译但float不是:
class MY_CONSTS
{
public :
static const long LONG_CONST = 1; // Compiles
static const float FLOAT_CONST = 0.001f; // C2864
};
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
1>c:\projects\myproject\Constant_definitions.h(71) : error C2864: 'MY_CONSTS::FLOAT_CONST' : only static const integral data members can be initialized within a class
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
为什么static const成员不能在本地课程中存在的原因是什么?这似乎是一个相当愚蠢的限制.
例:
void foo() {
struct bar {
int baz() { return 0; } // allowed
static const int qux = 0; // not allowed?!?
};
}
struct non_local_bar {
int baz() { return 0; } // allowed
static const int qux = 0; // allowed
};
Run Code Online (Sandbox Code Playgroud)
从标准报价(9.8.4):
本地类不应具有静态数据成员.
在C++ 11和C++ 14中,为什么我需要constexpr在以下代码段中:
class Foo {
static constexpr double X = 0.75;
};
Run Code Online (Sandbox Code Playgroud)
而这个产生编译错误:
class Foo {
static const double X = 0.75;
};
Run Code Online (Sandbox Code Playgroud)
(更令人惊讶的是)这个编译没有错误?
class Foo {
static const double X;
};
const double Foo::X = 0.75;
Run Code Online (Sandbox Code Playgroud) 以下代码没问题:
constexpr double square_cstxpr(double x) { return x * x; }
int main() {
const int test = 5;
constexpr double result = square_cstxpr((double)test);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果类型test从更改const int为const double,则g ++会出现以下错误:the value of 'test' is not usable in a constant expression.
请在此处查看g ++的代码和输出:http://coliru.stacked-crooked.com/a/2fe9b176c2b23798
有人可以解释一下这种行为吗?
当我尝试在类定义中初始化int成员变量时,我的C++编译器会抱怨.它告诉"只能在类中初始化静态const积分数据成员".能否请您解释这一限制背后的基本原理(如果可能,请举例说明).
如果我这样做的话
class Gone
{
public:
static const int a = 3;
}
Run Code Online (Sandbox Code Playgroud)
它有效,但如果有的话
class Gone
{
public:
static int a = 3;
}
Run Code Online (Sandbox Code Playgroud)
它给出了编译错误.现在我知道为什么第二个不起作用,我只是不知道为什么第一个这样做.
提前致谢.
struct Example
{
static const int One = 1000; // Legal
static const short Two = 2000; // Illegal
static const float Three = 2000.0f; // Illegal
static const double Four = 3000.0; // Illegal
static const string Five = "Hello"; // Illegal
};
Run Code Online (Sandbox Code Playgroud)
有没有理由认为#2,#3,#4和#5是非法的?
我想我知道#5的原因:编译器需要一个"真正的"字符串对象(因为它不是内置类型)并且不能无意义地替换为就好Five了.但如果是这种情况,编译器是否不能在.obj文件中留下提示并告诉链接器自动创建某个地方的某个实例?"Hello"#define Five "Hello"string Five
对于#3和#4,特别是#2(哈哈!)......我真的看不出任何可能的原因!浮点数和双打数是内置类型,就像int一样!而short只是一个(可能)更短的整数.
编辑:我正在使用Visual Studio 2008来编译它.我认为在这种情况下所有编译器的行为都相同,但显然g ++编译得很好(#5除外).VS为这些片段提供的错误是:
error C2864: 'Example::Two' : only static const integral data members can be initialized within a class
error C2864: … 我知道你不能在不使用构造函数的情况下直接在类中初始化成员变量(静态常量除外)。
但只是想知道这背后的原因是什么。下面是代码片段
如果有任何机构可以提供帮助
class a
{
int c=5;
// giving error error C2864: 'a::c' : only static const integral data members can be
// initialized within a class
int b;
public:
a():c(1),b(2){}
void h()
{
printf("%d,%d",c,b);
}
};
int main()
{
a l;
l.h();
getchar();
}
Run Code Online (Sandbox Code Playgroud) C++强制程序员在类外部定义一个非常量静态成员,我不断看到的原因是如果静态成员是在类中定义的,这将导致静态成员的多个定义.我知道有一个静态成员的多个定义是坏的,但我不明白这些多个定义甚至会来自何处.初始化的非常量静态成员不应该只进入数据部分并且这是唯一的定义吗?
struct Student {
static int x = 4; // Why would this result in multiple definitions?
};
Run Code Online (Sandbox Code Playgroud)
另外,我在其他stackoverflow文章中读到,const静态成员只是在使用它的地方简单地内联到代码中: 为什么我不能在类中有一个非整数的静态const成员? 这是由预处理器和所有其他指令处理的吗?(如果需要,我会在另一篇文章中提出这个问题,但我不确定它是否值得单独发帖).
在C++中,有没有办法以类似于嵌套typedef的方式定义类中的嵌套宏/常量,或者实现类似功能的方法?动机通常是模板使用的宏.
class SomeClass
{
public:
#define SomeConstant 123
};
int x=SomeClass::SomeConstant;
Run Code Online (Sandbox Code Playgroud)
当然,静态const成员可以完成这项工作,但那些是物理变量,而我正在寻找一种简单的类宏行为.
c++ ×10
static ×3
c++11 ×2
const ×2
c ×1
c++14 ×1
constexpr ×1
local-class ×1
macros ×1
member ×1
nested ×1
redefinition ×1
visual-c++ ×1