使用 C++/CLI 进行 100% 托管开发有哪些优势?
使用 C++/CLI 进行100%托管开发(即使用 /clr:safe “生成...程序集,就像用... C#编写的程序集”)?尤其是与 C# 相比(注意 C++/CLI:相对于 C# 的优势 和 使用 C++ 有什么优势吗/CLI 通过标准 C++ 或 C#? 主要是关于托管/非托管互操作)。
例如,以下是我的一些想法:
托管类型的 C++ 风格引用,不如成熟的non那么优雅-nullable 引用,但总比没有好,或者使用 解决方法。
比泛型更强大的模板
比预处理器(这可能是一个缺点!,但宏对于代码生成很有用)
引用类型的堆栈语义--自动调用IDisposable::Dispose()
通过 C++ 析构函数更轻松地实现 Dispose()
C# 3.0 添加了自动实现的属性,因此这不再是 C++/CLI 的优势。
What are the advantages (the list of possible disadvantages is lenghtly) of doing 100% managed development using C++/CLI (that is, compile with /clr:safe which "generates ... assemblies, like those written in ... C#")? Especially when compard to C# (note C++/CLI : Advantages over C# and Is there any advantage to using C++/CLI over either standard C++ or C#? are mostly about managed/unmanaged interop).
For example, here are a few off the top of my head:
C++-style references for managed types, not as elegant as full blown non-nullable references but better than nothing or using a work-around.
templates which are more powerful than generics
preprocessor (this may be a disadvantage!, but macros can be useful for code generation)
stack semantics for reference types--automatically calling IDisposable::Dispose()
easier implementation of Dispose() via C++ destructor
C# 3.0 added auto-implemented properties, so that is no longer a C++/CLI advantage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为最大的优势是托管/非托管互操作。编写纯托管 C++/CLI(至少对我来说)而不与 C# 或其他 .Net 语言互操作似乎完全没有抓住重点。是的,你可以这样做,但你为什么要这么做呢?
如果您要编写纯托管代码,为什么不使用 C#。特别是(就像 nobugs 所说的那样)如果 VS2010 放弃对 C++/CLI 的 IntelliSense 支持。另外,在 VS2008 中,C++/CLI 的 IntelliSense 不如 C# IntelliSense;因此,从开发人员的角度来看,在 C# 中工作/探索/重构比在 C++/CLI 中更容易。
如果您想要您列出的一些 C++ 优点,例如预处理器、堆栈语义和模板,那么为什么不使用 C++?
I would think that the single biggest advantage is the managed/unmanaged interop. Writing pure managed C++/CLI would (to me at least) without interoping with C# or other .Net languages seems like missing the point entirely. Yeah you could do this, but why would you?
If you're going to write pure managed code why not use C#. Especially (like nobugs said) if VS2010 drops IntelliSense support for C++/CLI. Also in VS2008 the IntelliSense for C++/CLI isn't as good the C# IntelliSense; so from a developer standpoint, it's easier to work/explore/refactor in C# than C++/CLI.
If you want some of the C++ benefits you list like the preprocessor, stack semantics and templates, then why not use C++?
奇怪的是,我喜欢 C++/CLI,但你恰恰列出了我不喜欢的功能。我的批评:
嗯,这也许都是非常主观的。 VS2010 将会敲响丧钟,它将不提供对 C++/CLI 的 IntelliSense 支持。
Odd, I like C++/CLI but you listed exactly its features I dislike. My criticisms:
Well, that's all very subjective perhaps. The death-knell will come with VS2010, it will ship without IntelliSense support for C++/CLI.
在 C++/CLI 中,您可以在类之外定义函数,但在 C# 中则不能这样做。但不知道这算不算优点
In C++/CLI you can define functions outside of classes, you can't do that in C#. But I don't know if that is an advantage
和这里的其他人一样,我想不出任何存在明显优势的一般情况,所以我的想法转向了情境优势——是否有在特定场景下存在优势的情况?
优点:在快速原型设计场景中利用技术人员的 C++ 技能。
让我详细说明一下......
我曾与未经过正式培训的程序员的科学家和(非软件)工程师合作过很多次。其中许多人使用 C++ 开发涉及高端物理/数学的特定模块。如果快速原型设计场景需要纯 .NET 模块,并且负责该模块的科学家/工程师的技能是 C++,我会教他们少量额外的语法(
public ref
,^
和%
和gcnew
) 并让他们将其模块编程为 100% 托管的 C++/CLI DLL。我认识到可能会有一大堆“是的,但是……”的回答,但我认为利用技术人员的 C++ 技能是 C++/CLI 的一个可能优势。
Like others here, I can't think of any general cases where a clear advantage exists, so my thinking turned to situational advantages -- are there any cases where there is an advantage in a particular scenario?
Advantage: Leverage the C++ skill set of technical staff in a rapid prototyping scenario.
Let me elaborate ...
I have worked quite a bit with scientists and (non-software) engineers who aren't formally trained programmers. Many of these people use C++ for developing specific modules involving high-end physics/mathematics. If a pure .NET module is required in a rapid prototyping scenario and the skill set of the scientist/engineer responsible for the module is C++, I would teach them a small amount of additional syntax (
public ref
,^
and%
andgcnew
) and get them to program up their module as a 100% managed C++/CLI DLL.I recognize there are a whole heap of possible "Yes, but ..." responses, but I think leveraging the C++ skill set of technical staff is a possible advantage of C++/CLI.
我同意您所提到的内容,作为预处理器使用的示例指向: Boost 预处理器库,用于基于 C++/CLI 中的基本类型列表(例如 PointI32、PointF32 等)生成一组类型
I agree on what you have mentioned and as an example of preprocessor use point to: Boost Preprocessor library for generating a set of types based on a list of basic types e.g. PointI32, PointF32 etc. in C++/CLI
您可以将枚举和委托作为 C++/CLI 中的通用约束,但不能在 C# 中。
https://connect.microsoft .com/VisualStudio/feedback/details/386194/allow-enum-as-generic-constraint-in-c
有一个库可以在 C# 中模拟这些约束。
http://code.google.com/p/unconstrained-melody/
You can have enums and delegates as generic constraints in C++/CLI, but not in C#.
https://connect.microsoft.com/VisualStudio/feedback/details/386194/allow-enum-as-generic-constraint-in-c
There is a library to simulate these constraints in C#.
http://code.google.com/p/unconstrained-melody/
人们可以想象一个假设的产品有以下要求:
在这种情况下,使用例如 C# 来实现 1 会阻碍您实现 2和 3 没有重写。因此,可以使用 C++/CLI 进行开发,适当地使用宏和模板恶作剧,使其看起来尽可能像普通的 C++,达到要求 1,然后达到要求 2,需要 (a) 重新实现所述宏和模板恶作剧映射到 pukka C++ 并 (b) 实现 pukka C++ 中使用的 .NET 框架类。请注意,(a)和(b)一旦完成一次就可以在将来重复使用。
最明显的反对意见是“那为什么不在本机 C++ 中完成整个事情呢?”;也许在庞大的 .NET 类库中有很多好东西,您希望使用它们来尽快推向市场。
我承认这有点脆弱,所以我非常怀疑这是否曾经被做过,但尝试一下会是一件有趣的事情!
One could imagine the following requirements for a hypothetical product:
In such a scenario, using eg C# for 1 would stymie you on 2 and 3 without a rewrite. So, one could develop in C++/CLI, suitably munged with macros and template shenanigans to look as much like ordinary C++ as possible, to hit reqt 1, then to hit reqt 2 one would need to (a) reimplement said macros and template shenanigans to map to pukka C++ and (b) implement .NET framework classes used in pukka C++. Note that (a) and (b) could be reused in future once done once.
The most obvious objection would be "well why not do the whole thing in native C++ then?"; well maybe there's lots of good stuff in the vast .NET class library that you want to use to get to market asap.
All a bit tenuous I admit, so I very much doubt this has ever been done, but it'd be a fun thing to try out !