在__nogc类中包含__value类实例变量 [英] Having __value class instance variables in __nogc classes

查看:90
本文介绍了在__nogc类中包含__value类实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在__nogc

类中有一个__value类实例变量是不可能的,因为可以在

__nogc中有一个内置类型实例变量class?


__value class X {int a; };

__nogc class Y {X b; int c;}; //编译错误

__nogc class Y {int c;}; // OK

Why is it impossible to have a __value class instance variable in a __nogc
class when it is possible to have a built-in type instance variable in a
__nogc class ?

__value class X { int a; };
__nogc class Y { X b; int c;}; // compiler error
__nogc class Y { int c;}; // OK

推荐答案

如果值类型没有直接或

间接包含任何值,那么_would_可以允许这样做ref类型。但语言设计选择了

通过不允许任何情况简化了这一点。


Ronald Laeremans

Visual C ++团队


" Edward Diener" < ED ****** @ tropicsoft.com>在留言中写道

news:u2 ************** @ TK2MSFTNGP09.phx.gbl ...
It _would_ be possible to allow this if the value type does not directly or
indirectly contain any ref types. But the language design made the choice to
simplify this by not allowing any cases.

Ronald Laeremans
Visual C++ team

"Edward Diener" <ed******@tropicsoft.com> wrote in message
news:u2**************@TK2MSFTNGP09.phx.gbl...
为什么不可能在__nogc
类中有一个__value类实例变量,可以在
__nogc类中有一个内置类型实例变量吗?

__value class X {int a ; };
__nogc类Y {X b; int c;}; //编译错误
__nogc class Y {int c;}; //好吧
Why is it impossible to have a __value class instance variable in a __nogc
class when it is possible to have a built-in type instance variable in a
__nogc class ?

__value class X { int a; };
__nogc class Y { X b; int c;}; // compiler error
__nogc class Y { int c;}; // OK



嗨罗纳德,
如果值类型没有,那么_would_可以允许这个直接
或间接包含任何ref类型。但语言设计通过不允许任何情况选择
来简化这一点。
It _would_ be possible to allow this if the value type does not directly or indirectly contain any ref types. But the language design made the choice to simplify this by not allowing any cases.




唉,这并不完全正确,就像这个简单的例子显示:


#using< mscorlib.dll>

使用命名空间系统;

使用命名空间System :: Runtime :: InteropServices;


[StructLayout(LayoutType :: Sequential)]

__value struct X {int a; };

__nogc类Y {X x; int c; };

所以,显然,规则并不像在所有情况下那样简单。

AFAIK,规则更接近于此:允许iif:

- X没有ref类型的成员

- X已明确声明为顺序布局(实际上应该是

不必要)

- X在与Y相同的程序集中声明(或者可能是相同的翻译

单位?还没有详细检查过。)


但是,是的,出于所有用心的目的,人们应该认为这是禁止的;)

-

Tomas Restrepo
到**** @ mvps.org


Ronald Laeremans [MSFT]写道:
Ronald Laeremans [MSFT] wrote:
如果值类型没有直接或间接包含任何ref类型,则_would_可以允许这样做。但是语言设计通过不允许任何情况做出选择来简化这一点。


我可以理解反对任何参考类型的规则,但我不会理解简化选择。我发现它在__nogc类的混合

模式MC ++开发中经常有价值,它有一个相当简单的__value

对象作为类的成员。令人失望的是,这不是允许的b $ b,特别是因为内置类型本身就是__value类而且

__nogc类当然可以有一个内置对象。 br />
Ronald Laeremans
Visual C ++团队

Edward Diener < ED ****** @ tropicsoft.com>在消息中写道
新闻:u2 ************** @ TK2MSFTNGP09.phx.gbl ...
It _would_ be possible to allow this if the value type does not
directly or indirectly contain any ref types. But the language design
made the choice to simplify this by not allowing any cases.
I can understand the rule against having any ref types but I don''t
understand the simplifying choice. I have found it often valuable in mixed
mode MC++ development for the __nogc class to have a fairly simple __value
object as a member of the class. It is disappointing that this is not
allowed especially as built-in types are __value classes themselves and
__nogc classes can, of course, have a built-in object.

Ronald Laeremans
Visual C++ team

"Edward Diener" <ed******@tropicsoft.com> wrote in message
news:u2**************@TK2MSFTNGP09.phx.gbl...
为什么不能有一个__value类当__nogc类中有一个内置类型实例变量时,
__nogc类中的实例变量?

__ value类X {int a; };
__nogc类Y {X b; int c;}; //编译错误
__nogc class Y {int c;}; //确定
Why is it impossible to have a __value class instance variable in a
__nogc class when it is possible to have a built-in type instance
variable in a __nogc class ?

__value class X { int a; };
__nogc class Y { X b; int c;}; // compiler error
__nogc class Y { int c;}; // OK



这篇关于在__nogc类中包含__value类实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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