Bjarne Stroustrup on C++/CLI
I hadn’t seen this before; Bjarne Stroustrup’s FAQ has an entry that describes his position on C++/CLI. It’s worth reading.
In a nutshell, he acknowledges that to use the BCL and write .NET classes in .NET you need language features that C++ doesn’t have. So you need something like C++/CLI.
But he also makes it clear that you don’t want code that uses C++/CLI extensions scattered throughout your application.
Well, maybe you do. These features are there for a reason - because they make development easier - but using them ties your application to the CLR.
It’s possible to write a managed application in C++ and still maintain separation between the portable C++ and the .NET specific C++.
And if you’re going to do that, my suggestion would be to write the managed portion in C#.
If you know C++ you already mostly know C#, and writing the .NET specific portion of your app in C# and calling it from the C++ code makes the separation clear.
September 9th, 2006 at 12:13 pm
I think what Bjarne is trying to say is to have an abstraction layer of C++/CLI code that provides access to ISO C++ code (i.e. code that is not publicly available at the assembly level or uses .NET classes). C++/CLI will perform all the PInvoke between the CLI class and the ISO C++ classes automatically with C++ interop.
This, of course, depends on your goals. If you *never* plan on using the ISO C++ code in anything other than managed application, forcing it the be PInvoked adds a slight performance hit for no reason. Arguably a premature optimization to push C++ code that does not access .NET classes or CLI features into C++/CLI…
Although Bjarne puts a heavy preference to writing “portable” code, using ISO C++ over C++/CLI forces the code to be compiled for a specific processor, making the assembly essentially not portable, depending on the platform to which it is deployed.