使用 C++/CLI 进行 100% 托管开发有哪些优势?

发布于 2024-08-19 20:22:03 字数 1147 浏览 4 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

大姐,你呐 2024-08-26 20:22:03

我认为最大的优势是托管/非托管互操作。编写纯托管 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++?

菩提树下叶撕阳。 2024-08-26 20:22:03

奇怪的是,我喜欢 C++/CLI,但你恰恰列出了我不喜欢的功能。我的批评:

  • 好吧。但意外使用帽子的情况相当普遍,在没有警告的情况下将值类型的值装箱。没有办法诊断这个错误。
  • 强大的功能需要付出高昂的代价,您编写的模板无法在任何其他 .NET 语言中使用。如果说有什么不同的话,那就是它加剧了 C++ 模板导出问题。 STL/CLR的彻底失败也值得深思。
  • 呃,不。
  • 在我看来,这是一个严重的错误。正如第一个要点所述,意外拳击的问题已经很难避免。堆栈语义使得任何新手程序员都很难解决这个问题。这是为了安抚 C++ 程序员而做出的设计决定,没关系,但 using 语句是更好的解决方案。
  • 不知道如何更容易。 GC.SuppressFinalize() 调用是自动的,仅此而已。很少有人编写终结器,但您无法避免自动生成的代码进行调用。这是低效的,并且违反了“不使用不用付费”的原则。除此之外,编写析构函数还会强制自动生成默认的终结器。如果您忘记或忽略使用析构函数,您永远不会使用并且不想使用它。

嗯,这也许都是非常主观的。 VS2010 将会敲响丧钟,它将不提供对 C++/CLI 的 IntelliSense 支持。

Odd, I like C++/CLI but you listed exactly its features I dislike. My criticisms:

  • Okay. But accidental use of the hat is pretty widespread, getting the value of the value type boxed without warning. There is no way to diagnose this mistake.
  • Power that comes at a high price, templates you write are not usable in any other .NET language. If anything, it worsens the C++ template export problem. The complete failure of STL/CLR is worth pondering too.
  • Erm, no.
  • This was IMO a serious mistake. It already is difficult to avoid problems with accidental boxing, as outlined in the first bullet. Stack semantics makes it seriously difficult for any starting programmer to sort this out. This was a design decision to placate C++ programmers, that's okay, but the using statement was a better solution.
  • Not sure how it is easier. The GC.SuppressFinalize() call is automatic, that's all. It is very rare for anybody to write a finalizer, but you can't avoid the auto-generated code from making the call. That's inefficient and a violation of the 'you don't pay for what you don't use' principle. Add to this that writing the destructor also forces a default finalizer to be auto-generated. One you'd never use and wouldn't want to be used if you forgot or omitted to use the destructor.

Well, that's all very subjective perhaps. The death-knell will come with VS2010, it will ship without IntelliSense support for C++/CLI.

战皆罪 2024-08-26 20:22:03

在 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

葮薆情 2024-08-26 20:22:03

和这里的其他人一样,我想不出任何存在明显优势的一般情况,所以我的想法转向了情境优势——是否有在特定场景下存在优势的情况?

优点:在快速原型设计场景中利用技术人员的 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 % and gcnew) 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.

给不了的爱 2024-08-26 20:22:03

您可以将枚举和委托作为 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/

彡翼 2024-08-26 20:22:03

人们可以想象一个假设的产品有以下要求:

  1. 在 Windows 上快速上市
  2. 最终部署到非 Windows 平台
  3. 不得在非 Windows 平台上依赖 Mono

在这种情况下,使用例如 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:

  1. Quick time-to-market on Windows
  2. Eventual deploy to non-Windows platforms
  3. Must not rely on Mono for non-Windows

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文