为什么C ++语言需要修改为"管理"? [英] Why does C++ need language modifications to be "managed"?

查看:136
本文介绍了为什么C ++语言需要修改为"管理"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能编译器写的管理的什么需要的管理的C ++中code(即使其CLR兼容)<? / P>

也许有一些妥协,比如禁止的无效的指针,在某些情况下等,但所有这些额外的关键字等,那是必须由这些增加要解决的问题?

我有我的想法的一些方面,什么可能很难解决,但一个好的坚实的解释将是非常美联社preciated!

解决方案

我有答案不同意为止。

的主要问题理解的是,一个C ++编译器创建code,适用于一个非常愚蠢的环境。即使是现代的CPU不知道虚函数,地狱,连功能都一气。一个CPU真的不关心异常处理code放松堆栈任何功能之外,例如。 CPU的交易中的指令序列,跳跃和回报。功能肯定没有名字至于CPU而言。

因此​​,这是需要的,以支持一个函数的概念一切是由编译器放在那里。例如。虚函数表是正确的大小只是阵列,从CPU的角度来看正确的价值观。 __ __ FUNC 最终为一个序列中的字符串表的字节,其中最后一个是00

现在,没有什么,说目标环境的必须是哑巴的。你肯定可以针对JVM。同样,编译器必须填写什么没有原生提供。没有原始内存?然后分配一个大的字节数组,并用它来代替。没有原始指针?只要使用整数索引到那么大的字节数组。

主要的问题是,C ++程序看起来与托管环境相当面目全非。 JVM是不是哑巴,它知道的功能,但它希望他们是类成员。它不指望他们有&LT; &GT; 在他们的名字。您可以规避这一点,但你最终基本上是从名字粉碎。不像名字今天重整,这种名称改编的无意对C接头,但对于智能环境。因此,它的反射引擎可以确信,有一类 c__plus__plus 与成员函数 __ namespace_std__for_each__arguments_int_pointer_int_pointer_function_address ,这仍是一个不错的例。我不想知道,如果你有一个的std ::地图字符串来扭转迭代器会发生什么。

周围的其他方法实际上是轻松了许多,一般。 pretty的其他语言的多抽象都可以按摩走在C ++中。垃圾收集?这已经获准在C ++的今天,这样你就可以支持,即使对于无效*

有一件事情我还不解决的是性能。仿效原始内存在一个大的字节数组?这不会是快速的,特别是如果你把双打在其中。你可以玩一大堆把戏,使其更快,但它的价格?你可能不会得到商业上可行的产品。事实上,你可能与它结合了C ++(许多不寻常的实现相关的行为)与VM(慢)。

中最糟糕的部分中最糟糕的部分的语言

Why can't a compiler be written that manages what needs to be managed in C++ code (i.e. to make it "CLR compatible")?

Maybe with some compromise, like prohibiting void pointers in some situations etc. But all these extra keywords etc. What's the problem that has to be solved by these additions?

I have my thoughts about some aspects and what might be hard to solve, but a good solid explanation would be highly appreciated!

解决方案

I'd have to disagree with the answers so far.

The main problem to understand is that a C++ compiler creates code which is suitable for a very dumb environment. Even a modern CPU does not know about virtual functions, hell, even functions are a stretch. A CPU really doesn't care that exception handling code to unwind the stack is outside any function, for instance. CPU's deal in instruction sequences, with jumps and returns. Functions certainly do not have names as far as the CPU is concerned.

Hence, everything that's needed to support the concept of a function is put there by the compiler. E.g. vtables are just arrays of the right size, with the right values from the CPUs viewpoint. __func__ ends up as a sequence of bytes in the string table, the last one of which is 00.

Now, there's nothing that says the target environment has to be dumb. You could definitely target the JVM. Again, the compiler has to fill in what's not natively offered. No raw memory? Then allocate a big byte array and use it instead. No raw pointers? Just use integer indices into that big byte array.

The main problem is that the C++ program looks quite unrecognizable from the hosting environment. The JVM isn't dumb, it knows about functions, but it expects them to be class members. It doesn't expect them to have < and > in their name. You can circumvent this, but what you end up with is basically name mangling. And unlike name mangling today, this kind of name mangling isn't intended for C linkers but for smart environments. So, its reflection engine may become convinced that there is a class c__plus__plus with member function __namespace_std__for_each__arguments_int_pointer_int_pointer_function_address, and that's still a nice example. I don't want to know what happens if you have a std::map of strings to reverse iterators.

The other way around is actually a lot easier, in general. Pretty much all abstractions of other languages can be massaged away in C++. Garbage collection? That's already allowed in C++ today, so you could support that even for void*.

One thing I didn't address yet is performance. Emulating raw memory in a big byte array? That's not going to be fast, especially if you put doubles in them. You can play a whole lot of tricks to make it faster, but at which price? You're probably not going to get a commercially viable product. In fact, you might up with a language that combines the worst parts of C++ (lots of unusual implementation-dependent behavior) with the worst parts of a VM (slow).

这篇关于为什么C ++语言需要修改为&QUOT;管理&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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