why c++? isn’t c# enough? kate gregory gregory consulting

8
Why C++? Isn’t C# enough? Why C++? Isn’t C# enough? Kate Gregory Kate Gregory Gregory Consulting Gregory Consulting

Upload: baldwin-newton

Post on 22-Dec-2015

220 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Why C++? Isn’t C# enough? Kate Gregory Gregory Consulting

Why C++? Isn’t C# enough?Why C++? Isn’t C# enough?

Kate GregoryKate GregoryGregory ConsultingGregory Consulting

Page 2: Why C++? Isn’t C# enough? Kate Gregory Gregory 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

Page 3: Why C++? Isn’t C# enough? Kate Gregory Gregory Consulting

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 } }}

Page 4: Why C++? Isn’t C# enough? Kate Gregory Gregory Consulting

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}

Page 5: Why C++? Isn’t C# enough? Kate Gregory Gregory Consulting

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

Page 6: Why C++? Isn’t C# enough? Kate Gregory Gregory Consulting

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

Page 7: Why C++? Isn’t C# enough? Kate Gregory Gregory Consulting

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

Page 8: Why C++? Isn’t C# enough? Kate Gregory Gregory Consulting

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