What are the advantages of C++/CLI (formerly "Managed C++") over standard C++?
The closest question I found in SO was this开发者_StackOverflow中文版 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++.
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.
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.
The principal benefit is access to the .net framework and other code written in other .net languages.
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.)
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.
精彩评论