與標準 C++ 相比,C++/CLI(以前稱為“託管 C++”)有哪些優勢? (What are the advantages of C++/CLI (formerly "Managed C++") over standard C++?)


問題描述

與標準 C++ 相比,C++/CLI(以前稱為“託管 C++”)有哪些優勢? (What are the advantages of C++/CLI (formerly "Managed C++") over standard C++?)

The closest question I found in SO was this one, but the answers were really referring to advantages of C++/CLI over C#.

I need to understand the advantages of C++/CLI over standard C++.


參考解法

方法 1:

C++/CLI and Managed C++ (or Managed Extensions for C++) are two different things. Managed C++ is the previous version, and has some syntactical differences. It is also deprecated in favor of C++/CLI.

C++/CLI is an extension of ISO C++, so the comparison is moot. It allows writing code that will run on the CLR. As such, whether or not you want to use that extension depends on whether or not you need to write something in C++ that also needs to interface with a CIL language (C#, for instance).

For example, if you need to use a legacy library written in C or C++ within a .NET application, you can write a C++/CLI wrapper for it.

方法 2:

One thing I haven't seen mentioned in the answers yet (probably because it's more of a disadvantage, but it's a relevant consideration) is that C++/CLI tends to get second-class treatment. Microsoft used to push it as pretty much a replacement for C++. they wanted native developers to switch to .NET, and the way to do that was to write C++/CLI instead of C++.

Now, they've abandoned this, and C++/CLI is relegated to a role as an interop language. Microsoft recommends that you use it when you need to mix native C++ and .NET code, you may use C++/CLI as a "bridge" between them, while C++ is once again a first-class language.

C++/CLI has also fallen behind in certain ways:

  • In Visual Studio 2010, Intellisense no longer worked for C++/CLI. It will be reenabled at some point, but for now, it's gone.
  • VS2010 adds support for parts of C++0x, while, as far as I know, not all of it works with C++/CLI yet. Again, the assumption is that Microsoft keeps the two languages in sync, but there can be delays when new features are added to the C++ language, before they're made available in C++/CLI.

So if you want to write .NET code, use a "real" .NET language such as C#. If you want to write C++, use the "real" native C++. And if you want to mix the two, use C++/CLI to write the interop code.

方法 3:

The principal benefit is access to the .net framework and other code written in other .net languages.

方法 4:

Here is Bjarne Stroustrup's opinion of C++/CLI. I was surprised at this, because the last time I read it, it was far more negative. (It seems that Microsoft has now acceded to his plea that they maintain a clear distinction between C++/CLI and ISO C++ in all their documentation.)

方法 5:

  • It gets you access to terrific range of functionality in the .NET Base-Class-Library, etc, alongside what you can already do in C++.

  • If you're so inclined, you can have most of your code in pure C# assemblies and only use C++ where necessary.

(by Android EveEtienne de MarteljalfDavid HeffernanTonyKxyz)

參考文件

  1. What are the advantages of C++/CLI (formerly "Managed C++") over standard C++? (CC BY-SA 3.0/4.0)

#.net-3.5 #C++ #c++-cli #Windows






相關問題

如何在 C# 中搜索 pdf 中的文本(執行匹配) (How to search text (Exect match) in pdf in C#)

String.IsNullOrEmpty() (String.IsNullOrEmpty())

是否有可以綁定的可為空的日期選擇器? (Is there a nullable datepicker that I can bind to?)

具有自動命名屬性的通用組合框 (Generic ComboBox with automatically named properties)

在數據庫未定義的外鍵關係上配置實體框架 (Configuring Entity Framework on a Database Undefined Foreign Key Relationships)

我可以在 4.0 應用程序中引用 .NET 3.5 .DLL 嗎? (Can I reference a .NET 3.5 .DLL in a 4.0 app?)

如何在 ASP.NET、VB.NET 中解決這個會話問題? (How to tackle this session problem in ASP.NET,VB.NET?)

在 ADO.NET 數據服務中添加對查找表的引用 (Adding a reference to a lookup table in ADO.NET Data Services)

如何優化 Linq to Xml 查詢反對屬性? (How do I optimize a Linq to Xml query againist attributes?)

VS2005 和 LINQ (VS2005 and LINQ)

時區信息錯誤? (TimeZoneInfo error?)

與標準 C++ 相比,C++/CLI(以前稱為“託管 C++”)有哪些優勢? (What are the advantages of C++/CLI (formerly "Managed C++") over standard C++?)







留言討論