why c++? isn’t c# enough? kate gregory gregory consulting
TRANSCRIPT
Why C++? Isn’t C# enough?Why C++? Isn’t C# enough?
Kate GregoryKate GregoryGregory ConsultingGregory Consulting
What C++ has that others don’tWhat C++ has that others don’t
Can generate native codeCan generate native code
C++ interop – the fastest and easiestC++ interop – the fastest and easiest
Templates and genericsTemplates and generics
Deterministic destructionDeterministic destruction
Optimized MSILOptimized MSIL
PGO for native and managed codePGO for native and managed code
.NET Linking.NET Linking
Deterministic DestructionDeterministic Destruction
Nested “using”s in C# are hard to readNested “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 DestructionDeterministic Destruction
Automatic scope is easierAutomatic scope is easier
C++ destructor maps to CLR “Dispose”C++ destructor maps to CLR “Dispose”Both directionsBoth directions
Less code, more controlLess 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 OptimizationsManaged Code Optimizations
Compiler optimizes MSILCompiler optimizes MSILEven when generating verifiable codeEven when generating verifiable code
Some optimizations supported on MSILSome optimizations supported on MSILInliningInliningLoop unrollingLoop unrollingLoop invariant code motionLoop invariant code motionCommon subexpression eliminationCommon subexpression eliminationCopy propagationCopy propagationExpression optimizationsExpression optimizations
Whole program optimization now Whole program optimization now supported for /clr codesupported for /clr code
Compile with /GLSource Object files
Profile Guided OptimizationProfile Guided Optimization
Object files Link with /LTCG:PGI InstrumentedImage
Profile data
Object files
Link with /LTCG:PGOOptimized
Image
`̀InstrumentedImage
Scenarios Output Profile data
Using Other LanguagesUsing Other Languages
Visual C++ allows you to include code from Visual C++ allows you to include code from multiple languages in a single file assemblymultiple languages in a single file assembly
a.cppa.cpp
c.csc.cs
C++C++CompilerCompiler
EXEEXE
C++C++CodeCode
C#C#CodeCode
a.obja.obj
c.netmodulec.netmoduleC#C#
CompilerCompiler
C++C++LinkerLinker
D:\>cl /c /clr a.cpp
D:\>csc /t:module c.cs
C++ C++ CLR CLR
C++ FeaturesC++ Features
Deterministic cleanup, Deterministic cleanup, destructorsdestructors
TemplatesTemplates
Native typesNative types
Multiple inheritanceMultiple inheritance
STL, generic STL, generic algorithmsalgorithms
Pointer/pointee Pointer/pointee distinctiondistinction
Copy construction, Copy construction, assignmentassignment
CLR FeaturesCLR Features
Garbage collection, Garbage collection, finalizersfinalizers
GenericsGenerics
Reference and value Reference and value typestypes
InterfacesInterfaces
VerifiabilityVerifiability
SecuritySecurity
Properties, delegates, Properties, delegates, eventsevents