90 likes | 251 Views
Why C++? Isn’t C# enough?. Kate Gregory Gregory Consulting. What C++ has that others don’t. Can generate native code C++ interop – the fastest and easiest Templates and generics Deterministic destruction Optimized MSIL PGO for native and managed code .NET Linking.
E N D
Why C++? Isn’t C# enough? Kate Gregory Gregory Consulting
What C++ has that others don’t • Can generate native code • C++ interop – the fastest and easiest • Templates and generics • Deterministic destruction • Optimized MSIL • PGO for native and managed code • .NET Linking
Deterministic Destruction • Nested “using”s in C# are hard to read { using(SqlConnection conn = new SqlConnection(connString) ) { // work with the connection in some way using(SqlCommand cmd = new SqlCommand(queryString, conn) ) { // work with the command } } }
Deterministic Destruction • Automatic scope is easier • C++ destructor maps to CLR “Dispose” • Both directions • Less code, more control • { • SqlConnection conn(connString); • // work with the connection in some way • SqlCommand cmd(queryString, %conn); • // work with the command • // don’t call Dispose or Close • }
Managed Code Optimizations • Compiler optimizes MSIL • Even when generating verifiable code • Some optimizations supported on MSIL • Inlining • Loop unrolling • Loop invariant code motion • Common subexpression elimination • Copy propagation • Expression optimizations • Whole program optimization now supported for /clr code
Object files Compilewith /GL Source Object files Link with /LTCG:PGI Instrumented Image ` Scenarios Profile data Instrumented Image Output Profile data Link with /LTCG:PGO Optimized Image Object files Profile Guided Optimization
Using Other Languages • Visual C++ allows you to include code from multiple languages in a single file assembly a.cpp C++ Compiler a.obj EXE D:\>cl /c /clr a.cpp C++ Linker C++Code C#Code c.cs C# Compiler c.netmodule D:\>csc /t:module c.cs
C++ Features Deterministic cleanup, destructors Templates Native types Multiple inheritance STL, generic algorithms Pointer/pointee distinction Copy construction, assignment CLR Features Garbage collection, finalizers Generics Reference and value types Interfaces Verifiability Security Properties, delegates, events C++ CLR