为什么类定义中没有非整数静态const初始化器? [英] Why no non-integral static const initialiser's within class definition?

查看:75
本文介绍了为什么类定义中没有非整数静态const初始化器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在研究原因:


< example 1>


class ABC

{

static const float some_float = 3.3f;

};


< end example 1>


是不允许的。


发现


< example 2>


class ABC

{

static const float some_float;

};


const float some_float = 3.3f;


< end example 2>


是允许的。


这是正确的,为什么会这样?


-


干杯

-

Hewson :: Mike

这封信比平常更长,因为我没时间做它

更短 - Blaise Pascal

Have been researching as to why:

<example 1>

class ABC
{
static const float some_float = 3.3f;
};

<end example 1>

is not allowed.

Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed.

Is this correct and why is it so?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal

推荐答案



" Mike Hewson" <他****** @ optusnet.com.au>在消息中写道

"Mike Hewson" <he******@optusnet.com.au> wrote in message
一直在研究为什么:

<示例1>

类ABC
{
静态const浮动some_float = 3.3f;
};

<结束示例1>

是不允许的。


相关条款是9.4.2 / 4

"如果静态数据成员是const整数或const枚举类型,则其
类定义中的
声明可以指定一个常量初始化器,其中
应该是一个整型常量表达式。在这种情况下,成员可以在其范围内出现在整数常量表达式中。如果在程序中使用该成员,则仍应在命名空间范围内定义
,并且

命名空间范围定义不应包含初始化程序。


所以唯一的放松是对于静态const积分/枚举类型,你可以

如果你不在他们的程序中使用它们就可以逃脱定义,即

不要将它们视为l值。你缺少的关键点是

成员仍然可以出现在整数常量表达式中(如case标签)。

所以这个条款只是一个适用于积分的放宽常数

表达式。

发现

<示例2>

类ABC
{
static const float some_float;
};
const float some_float = 3.3f;

< end example 2>

>是允许的。
这是正确的,为什么会这样?
Have been researching as to why:

<example 1>

class ABC
{
static const float some_float = 3.3f;
};

<end example 1>

is not allowed.
The relevant clause is 9.4.2/4
"If a static data member is of const integral or const enumeration type, its
declaration in the class definition can specify a constant-initializer which
shall be an integral constant expression. In that case, the member can
appear in integral constant expressions within its scope. The member shall
still be defined in a namespace scope if it is used in the program and the
namespace scope definition shall not contain an initializer."

So the only relaxation is that for static const integral/enum types you can
get away with the definition if you don''t use them in their program i.e.
don''t treat them as l-values. The key point you are missing is that the
member can still appear in integral constant expressions (like case label).
So this clause is just a relaxation applicable only with integral constant
expressions.
Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed. Is this correct and why is it so?




不,即使这是不正确的。正确的方法是 -

const float ABC :: some_float = 3.3f;


Sharad



No, even this is incorrect. Correct way is -
const float ABC::some_float = 3.3f;

Sharad

Mike Hewson写道:
Mike Hewson wrote:
一直在研究为什么:
<示例1>
类ABC
{
static const float some_float = 3.3f;
};
<结束示例1>

是不允许的。
发现
< example 2>
class ABC
{
static const float some_float;
};
const float some_float = 3.3f;


应该是:

const float ABC :: some_float = 3.3f;

< end example 2>

是允许的。
这是正确的,为什么会这样?
Have been researching as to why:
<example 1>
class ABC
{
static const float some_float = 3.3f;
};
<end example 1>

is not allowed.
Found that
<example 2>
class ABC
{
static const float some_float;
};
const float some_float = 3.3f;
should be:
const float ABC::some_float = 3.3f;
<end example 2>

is allowed.
Is this correct and why is it so?




第二个例子是正确的。

为了能够做你在第一个例子中所做的事情,你的变量将需要符合以下要求:

必须是静态的。

必须是常量。

必须有一些整数或枚举类型。

初始化值必须是一个整数常量表达式。

最多可以初始化一次。如果你在

类中初始化它们,你就不能在它们的定义中再次初始化它们。


浮点类型违反了必须有一些积分或者

枚举类型要求。



The second example is correct.
To be able to do what you did in the first example you variable would
have to conform to the following set of requirements:
Must be static.
Must be const.
Must have some integral or enumeration type.
The initializing value must be an integral constant expression.
Can be initialized at most once. If you initialize them within the
class, you can''t initialize them again in their definitions.

The floating point types violate the Must have some integral or
enumeration type requirement.


Sharad Kala写道:
Sharad Kala wrote:
" Mike Hewson" <他****** @ optusnet.com.au>在消息中写道
相关的子句是9.4.2 / 4
如果一个静态数据成员是const整数或const枚举类型,它在类定义中的声明可以指定一个常量-initializer
应该是一个整数常量表达式。在这种情况下,成员可以在其范围内出现在整数常量表达式中。如果在程序中使用该成员,并且
命名空间范围定义不包含初始化程序,则该成员仍应在命名空间范围内定义。


是的,我找到了这个条款,但是我没看过/看看它是如何对非整数类型做出评论的。除非我的理解协处理器本周下降了


所以唯一的放松是静态const积分/枚举类型你可以躲开定义,如果你不在他们的程序中使用它们,即
不要将它们视为l值。


是的,他们是常数....所以不能成为l值,但我肯定会''使用

他们' '以某种方式分配给他们(否则创造它的重点是什么?)
你缺少的关键点是
成员仍然可以出现在整数常量表达式中(例如case标签)。
所以这个子句只是一个只适用于整数常量
表达式的松弛。

"Mike Hewson" <he******@optusnet.com.au> wrote in message
The relevant clause is 9.4.2/4
"If a static data member is of const integral or const enumeration type, its
declaration in the class definition can specify a constant-initializer which
shall be an integral constant expression. In that case, the member can
appear in integral constant expressions within its scope. The member shall
still be defined in a namespace scope if it is used in the program and the
namespace scope definition shall not contain an initializer."
Yeah, I''d found that clause, but I don''t read/see how it makes any
comment on non-integral types. Unless my comprehension co-processor is
down this week!

So the only relaxation is that for static const integral/enum types you can
get away with the definition if you don''t use them in their program i.e.
don''t treat them as l-values.
Yup, they''re const....so can''t be l-values, however I can surely ''use
them'' in a way that doesn''t assign to them ( otherwise what''s the point
in creating it? )
The key point you are missing is that the
member can still appear in integral constant expressions (like case label).
So this clause is just a relaxation applicable only with integral constant
expressions.

发现

<示例2>

类ABC
{
static const float some_float;
} ;

const浮动some_float = 3.3f;

<结束示例2>

是允许的。
Found that

<example 2>

class ABC
{
static const float some_float;
};

const float some_float = 3.3f;

<end example 2>

is allowed.





这是正确的,为什么会这样?
Is this correct and why is it so?



不,即使这是不正确的。正确的方法是 -
const浮动ABC :: some_float = 3.3f;


No, even this is incorrect. Correct way is -
const float ABC::some_float = 3.3f;




我认为


const浮动some_float = 3.3f;


没问题。 (前提是没有其他的some_float,范围是

混淆了事情)


无论如何,也许我应该改写:


为什么我可以在这种情境和方式中初始化*整数类型*而不是

任何其他基本类型(或用户定义的)?它是编译器

实现问题吗?


-


干杯

-

Hewson :: Mike

这封信比平时长,因为我没时间做它

short - Blaise Pascal



I think

const float some_float = 3.3f;

is fine. ( Provided there is no other some_float about, in scope to
confuse things )

In any case, perhaps I should rephrase:

Why can I initialise *integral types* in this context and manner but not
any other fundamental type ( or ''user'' defined )? Is it a compiler
implementation problem?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal


这篇关于为什么类定义中没有非整数静态const初始化器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆