7

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++.

Community
  • 1
  • 1
Android Eve
  • 14,864
  • 26
  • 71
  • 96
  • 3
    It has the outstanding benefit of limited portability to platforms that MS blesses... :/ – JimR Dec 26 '10 at 17:24

5 Answers5

13

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.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • thanks and +1 for pointing out that C++/CLI is not the same as Managed C++. So, do I understand correctly that the *only* advantage of C++/CLI over standard C++ is the ability to run on the CLR? – Android Eve Dec 26 '10 at 17:18
  • 1
    Yes. C++/CLI adds CLR compatibility at the cost of portability. – Etienne de Martel Dec 27 '10 at 01:01
11

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.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • Wow! This is a very well written and informative answer. Thanks and +1 (I would have given it a +5 but SO only allows +1). – Android Eve Dec 27 '10 at 15:35
3

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

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
3

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.)

TonyK
  • 16,761
  • 4
  • 37
  • 72
1
  • 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.

xyz
  • 27,223
  • 29
  • 105
  • 125