简体   繁体   English

使用C ++ / CLI进行100%托管开发有什么好处?

[英]What are the advantages of doing 100% managed development using 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#")? 使用C ++ / CLI进行100%托管开发有哪些优点(可能存在的缺点列表)(即使用/ clr:safe编译,“生成...程序集,就像用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). 特别是在考虑C#时(注意C ++ / CLI:优于C#的 优势,使用C ++ / CLI比标准C ++或C#更有优势吗?主要是关于托管/非托管互操作)。

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 . C ++ - 托管类型的样式引用 ,不像完整的非可空引用那样优雅,但总比没有好或使用解决方法

  • 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() 引用类型的堆栈语义 - 自动调用IDisposable :: Dispose()

  • easier implementation of Dispose() via C++ destructor 通过C ++析构函数更容易实现Dispose()

C# 3.0 added auto-implemented properties, so that is no longer a C++/CLI advantage. C#3.0添加了自动实现的属性,因此不再具有C ++ / CLI优势。

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. 在不使用C#或其他.Net语言的情况下编写纯托管C ++ / CLI(至少对我而言)似乎完全忽略了这一点。 Yeah you could do this, but why would you. 是的,你可以做到这一点,但你为什么要这样做。

If you're going to write pure managed code why not use C#. 如果您要编写纯托管代码,为什么不使用C#。 Especially (like nobugs said) if VS2010 drops IntelliSense support for C++/CLI. 特别是(如nobugs所说)如果VS2010放弃了对C ++ / CLI的IntelliSense支持。 Also in VS2008 the IntelliSense for C++/CLI isn't as good the C# IntelliSense; 同样在VS2008中,用于C ++ / CLI的IntelliSense不如C#IntelliSense好; so from a developer standpoint, it's easier to work/explore/refactor in C# than C++/CLI. 所以从开发人员的角度来看,在C#中工作/探索/重构比在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 ++的好处,比如预处理器,堆栈语义和模板,那么为什么不使用C ++呢?

Odd, I like C++/CLI but you listed exactly its features I dislike. 奇怪,我喜欢C ++ / CLI,但你确切地列出了我不喜欢的功能。 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. 高价的电源,您编写的模板不能用于任何其他.NET语言。 If anything, it worsens the C++ template export problem. 如果有的话,它会恶化C ++模板导出问题。 The complete failure of STL/CLR is worth pondering too. STL / CLR的完全失败也值得深思。
  • Erm, no. 呃,不。
  • This was IMO a serious mistake. 这是IMO的一个严重错误。 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. 这是一个安抚C ++程序员的设计决定,没关系,但using语句是一个更好的解决方案。
  • Not sure how it is easier. 不知道如何更容易。 The GC.SuppressFinalize() call is automatic, that's all. GC.SuppressFinalize()调用是自动的,就是这样。 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. VS2010将会出现死亡之谜,它将在没有智能感知支持的情况下发布,用于C ++ / CLI。

In C++/CLI you can define functions outside of classes, you can't do that in C#. 在C ++ / CLI中,您可以在类之外定义函数,但不能在C#中执行此操作。 But I don't know if that is an advantage 但我不知道这是否有利

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. 优势:在快速原型设计方案中利用技术人员的C ++技能。

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. 其中许多人使用C ++开发涉及高端物理/数学的特定模块。 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. 如果在快速原型设计场景中需要纯.NET模块,并且负责该模块的科学家/工程师的技能集是C ++,我会教他们少量的附加语法( public ref^% and gcnew )和让他们将他们的模块编程为100%托管的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. 我认识到有一大堆可能的“是的,但是......”的回应,但我认为利用C ++技术人员的技能组合是C ++ / CLI的一个可能优势。

You can have enums and delegates as generic constraints in C++/CLI, but not in C#. 您可以将枚举和委托作为C ++ / CLI中的通用约束,但不能在C#中。

https://connect.microsoft.com/VisualStudio/feedback/details/386194/allow-enum-as-generic-constraint-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#. 有一个库可以在C#中模拟这些约束。

http://code.google.com/p/unconstrained-melody/ http://code.google.com/p/unconstrained-melody/

One could imagine the following requirements for a hypothetical product: 可以想象一个假设产品的以下要求:

  1. Quick time-to-market on Windows 在Windows上快速上市
  2. Eventual deploy to non-Windows platforms 最终部署到非Windows平台
  3. Must not rely on Mono for non-Windows 不能依赖Mono来实现非Windows

In such a scenario, using eg C# for 1 would stymie you on 2 and 3 without a rewrite. 在这种情况下,使用例如C#为1会在2和3时阻止你而不重写。 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++. 因此,可以在C ++ / CLI中开发,适当地使用宏和模板恶作剧来看起来尽可能像普通的C ++,来命令reqt 1,然后点击reqt 2,需要(a)重新实现所述宏和模板恶作剧映射到pukka C ++和(b)实现pukka C ++中使用的.NET框架类。 Note that (a) and (b) could be reused in future once done once. 请注意,一旦完成一次,(a)和(b)可以在将来重复使用。

The most obvious objection would be "well why not do the whole thing in native C++ then?"; 最明显的反对意见是“为什么不在原生C ++中完成整个事情呢?”; well maybe there's lots of good stuff in the vast .NET class library that you want to use to get to market asap. 好吧,也许在你想要用来尽快进入市场的庞大.NET类库中有很多好东西。

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 ! 我承认,这一点有点脆弱,所以我非常怀疑这一切都已经完成,但试一试会很有趣!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM