const成员 [英] const members

查看:61
本文介绍了const成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。如果我有一个包含const成员的结构类型:

struct mystruct

{

const int id;

int mutable;

};

定义

struct mystruct ms;

定义一个对象`ms ''部分* const? IE浏览器。对象`ms''

不是const整体,但修改ms.id会在所有上下文中产生UB?


2.提供答案上述问题是是。 (ms.id真的是

a const(sub)对象),下面的代码是否有效:


struct mystruct_without_const

{

int id;

int mutable;

};


struct mystruct * new_mystruct(void )

{

struct mystruct_without_const * msp;

msp = malloc(sizeof * msp);

if(b) msp)

{

msp-> id = / *生成id * /; / *定义良好* /

msp-> mutable = /*...*/;

}

return(struct mystruct * )msp;

}


(在上面的代码中我尝试创建一个非const结构对象,初始化

假设 - 以明确定义的方式成为const成员,并返回一个

指针指向某些成员被服务的对象。)


是mystruct和

mystruct_without_const之间有某种布局兼容性?


-

Stan Tobias

sed's / [AZ] // g''发送电子邮件

1. If I have a struct type which contains a const member:
struct mystruct
{
const int id;
int mutable;
};
does a definition
struct mystruct ms;
define an object `ms'' which is *partly* const? Ie. the object `ms''
is not const as a whole, but modifying ms.id yields UB in all contexts?

2. Provided the answer to the above question is "yes" (ms.id is really
a const (sub)object), would the below code work:

struct mystruct_without_const
{
int id;
int mutable;
};

struct mystruct *new_mystruct(void)
{
struct mystruct_without_const *msp;
msp = malloc(sizeof *msp);
if (msp)
{
msp->id = /*generate id*/; /*well defined*/
msp->mutable = /*...*/;
}
return (struct mystruct*)msp;
}

(In above code I try to create a non-const struct object, initialize
supposed-to-be-const members in a well defined manner, and return a
pointer to the object where some members are "constified".)

Is there some kind of layout compatibility between mystruct and
mystruct_without_const?

--
Stan Tobias
sed ''s/[A-Z]//g'' to email

推荐答案

" S.Tobias" < SN ******* @ amu.edu.pl>写道:
"S.Tobias" <sN*******@amu.edu.pl> wrote:
1.如果我有一个包含const成员的结构类型:
struct mystruct
{
const int id;
int mutable;
};
做一个定义
struct mystruct ms;
定义一个对象`ms'',它是* partial * const? IE浏览器。对象`ms''
不是const作为一个整体,但修改ms.id会在所有上下文中产生UB?


是的。因此,除非你初始化它,ms.id包含垃圾,不能给予

a可靠值,并且完全没用。

2.提供上述答案问题是是 (ms.id真的是一个const(sub)对象),下面的代码可以工作:

struct mystruct_without_const
{
int id;
int mutable;
};
mystruct和
mystruct_without_const之间是否存在某种布局兼容性?
1. If I have a struct type which contains a const member:
struct mystruct
{
const int id;
int mutable;
};
does a definition
struct mystruct ms;
define an object `ms'' which is *partly* const? Ie. the object `ms''
is not const as a whole, but modifying ms.id yields UB in all contexts?
Yes. So unless you initialise it, ms.id contains garbage, can''t be given
a reliable value, and is completely useless.
2. Provided the answer to the above question is "yes" (ms.id is really
a const (sub)object), would the below code work:

struct mystruct_without_const
{
int id;
int mutable;
}; Is there some kind of layout compatibility between mystruct and
mystruct_without_const?




是的。 6.2.5#25:

#类型的合格或非限定版本是不同类型

#属于同一类型类别且具有相同的表示

#和对齐要求.39)

#39)相同的表示和对齐要求意味着
#意味着可互换性作为函数的参数,返回值来自

#函数和工会成员。


我预见到你的方法存在一些问题;特别是,使用malloc()的
过于复杂,因为你可以指定(因此

用作返回值)结构,就像任何其他基本类型一样。


Richard



Yes. 6.2.5#25:
# The qualified or unqualified versions of a type are distinct types
# that belong to the same type category and have the same representation
# and alignment requirements.39)
# 39) The same representation and alignment requirements are meant to
# imply interchangeability as arguments to functions, return values from
# functions, and members of unions.

I foresee some problems with your method, though; in particular, that
using malloc() is over-complicated, since you can assign (and therefore
use as a return value) structs, just as any other basic type.

Richard


2004年11月16日星期二11:39:52 +0000,S.Tobias写道:
On Tue, 16 Nov 2004 11:39:52 +0000, S.Tobias wrote:
1.如果我有一个包含const成员的结构类型:
struct mystruct
{
const int id;
int mutable;
};
做一个定义
struct mystruct ms;
定义一个对象`ms''哪个是*部分* const?


这里有两个概念需要考虑,一个是const是否为

它的类型是否为const限定。 ms没有const限定的

类型。另一个是ms是否是可修改的。一个const对象是

不可修改,因为id有一个不可修改的成员,它是一个整体不可修改的
,这意味着像
这样的东西

ms = another_mystruct;


导致未定义的行为。

即。对象`ms''
不是const作为一个整体,但修改ms.id会在所有上下文中产生UB?


是修改ms和ms.id导致UB,后者不是直接的违反b $ b约束。然而,修改ms.mutable是可以的。

2.如果上述问题的答案是是,那么(ms.id真的是一个const(sub)对象),下面的代码可以工作:

struct mystruct_without_const
{
int id;
int mutable;
};

struct mystruct * new_mystruct(void)
结构mystruct_without_const * msp;
msp = malloc(sizeof * msp) ;
if(msp)
{
msp-> id = / *生成id * /; / *定义良好* /
msp-> mutable = /*...*/;
}
return(struct mystruct *)msp;
}

(在上面的代码中我尝试创建一个非const结构对象,以一种定义良好的方式初始化
应该成为const的成员,并返回一个
指向对象的指针有些成员是服用的。)

mystruct和
mystruct_without_const之间是否存在某种布局兼容性?
1. If I have a struct type which contains a const member:
struct mystruct
{
const int id;
int mutable;
};
does a definition
struct mystruct ms;
define an object `ms'' which is *partly* const?
There are 2 concepts to consider here, one is whether it is const i.e.
whether its type is const qualified. ms does not have a const qualified
type. The other is whether ms is "modifiable". A const object is
non-modifiable and because id has a non-modifiable member it is
non-modifiable as a whole, which means that things like

ms = another_mystruct;

result in undefined behaviour.
Ie. the object `ms''
is not const as a whole, but modifying ms.id yields UB in all contexts?
Yes modifying ms and ms.id result in UB where the latter is not a direct
constraint violation. Modifying ms.mutable is OK however.
2. Provided the answer to the above question is "yes" (ms.id is really
a const (sub)object), would the below code work:

struct mystruct_without_const
{
int id;
int mutable;
};

struct mystruct *new_mystruct(void)
{
struct mystruct_without_const *msp;
msp = malloc(sizeof *msp);
if (msp)
{
msp->id = /*generate id*/; /*well defined*/
msp->mutable = /*...*/;
}
return (struct mystruct*)msp;
}

(In above code I try to create a non-const struct object, initialize
supposed-to-be-const members in a well defined manner, and return a
pointer to the object where some members are "constified".)

Is there some kind of layout compatibility between mystruct and
mystruct_without_const?




不幸的是struct mystruct_without_const和struct mystruct是

不兼容的类型,这几乎意味着没有要求

表示一致性。


劳伦斯



Unfortunately struct mystruct_without_const and struct mystruct are
not compatible types, which pretty much means there are no requirements
for consistency of representation.

Lawrence


rl *@hoekstra-uitgeverij.nl (Richard Bos)在消息新闻中写道:< 41 *************** @ news.individual.net> ...
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<41***************@news.individual.net>...
" S.Tobias" < SN ******* @ amu.edu.pl>写道:
"S.Tobias" <sN*******@amu.edu.pl> wrote:
1.如果我有一个包含const成员的结构类型:
struct mystruct
{
const int id;
int mutable;
};
做一个定义
struct mystruct ms;
定义一个对象`ms'',它是* partial * const? IE浏览器。对象`ms''
不是const作为一个整体,但修改ms.id会在所有上下文中产生UB?
1. If I have a struct type which contains a const member:
struct mystruct
{
const int id;
int mutable;
};
does a definition
struct mystruct ms;
define an object `ms'' which is *partly* const? Ie. the object `ms''
is not const as a whole, but modifying ms.id yields UB in all contexts?



是的。因此,除非你初始化它,否则ms.id包含垃圾,不能给出可靠的值,并且完全没用。



Yes. So unless you initialise it, ms.id contains garbage, can''t be given
a reliable value, and is completely useless.

2.提供了答案以上问题是是 (ms.id真的是一个const(sub)对象),下面的代码可以工作:

struct mystruct_without_const
{
int id;
int mutable;
};
2. Provided the answer to the above question is "yes" (ms.id is really
a const (sub)object), would the below code work:

struct mystruct_without_const
{
int id;
int mutable;
};


mystruct和mystruct_without_const之间是否存在某种布局兼容性?
Is there some kind of layout compatibility between mystruct and
mystruct_without_const?


<是的。 6.2.5#25:
#类型的合格或非限定版本是不同类型
#属于同一类型类别且具有相同的表示
#和对齐要求.39)
#39)相同的表示和对齐要求意味着......意味着可互换性作为函数的参数,来自
#函数的返回值以及工会成员。
我预见到你的方法会遇到一些问题;特别是,使用malloc()过于复杂,因为你可以分配(因此
用作返回值)结构,就像任何其他基本类型一样。
Richard



Yes. 6.2.5#25:
# The qualified or unqualified versions of a type are distinct types
# that belong to the same type category and have the same representation
# and alignment requirements.39)
# 39) The same representation and alignment requirements are meant to
# imply interchangeability as arguments to functions, return values from
# functions, and members of unions.

I foresee some problems with your method, though; in particular, that
using malloc() is over-complicated, since you can assign (and therefore
use as a return value) structs, just as any other basic type.

Richard







我的理解是:

1.`struct mystruct ''和'mystruct_without_const''是不同的类型

2.Now 6.2.5#25说类型的合格或不合格版本......

这意味着(对我而言)


const struct mystruct的东西;

struct mystruct somethingElse;


有相同的表示和对齐要求......

等等:


const struct mystruct_without_const something2;

struct mystruct_without_const somethingElse2;


但这并不意味着(对我而言)两个结构具有相同的对齐

要求等。

我的解释错了吗?请e xplain。


问候,

Suman。



Hi,

What I understand is:
1.`struct mystruct'' and `mystruct_without_const'' are distinct types
2.Now 6.2.5#25 says "The qualified or unqualified versions of a type..."
which implies (for me) something like

const struct mystruct something;
struct mystruct somethingElse;

have "The same representation and alignment requirements ..."
and so do:

const struct mystruct_without_const something2;
struct mystruct_without_const somethingElse2;

But that does not imply(for me)that the two structs have same alignment
requirements etc.

Am I wrong in my interpretation?Please explain.

Regards,
Suman.


这篇关于const成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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