c++ when do you need to go native to c, c with c#? almost never if you are creating a new...

121

Upload: oswin-chase

Post on 23-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component
Page 2: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

C++ Scenarios for C# DevelopersEric BattalioSenior Program Manager, Microsoft

Page 3: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

C++ScenariosSome slides about C++11More Scenarios

Page 4: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component.

Page 5: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

You want to have fun developing againCoding closer to the metal is exciting, challenging, and fun. Expand your mind, pick up the STL, and learn from the C++ community.

Page 6: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

SymptomsBored writing the same old codeCurious about what C++ developers see in C++You want to feel the power of the STL and RAIIAttending C++ talks at Tech Ed

Page 7: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

ExamplesSuccessfully compiling your first post-Hello World appThat moment you start thinking in STLFollowing the C++ forums on Stack OverflowSpotting and fixing a bug in legacy C++ library

Page 8: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Demo

Hello World (of course)

Page 9: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

GotchasTemptation to become template-writing MasterLost time debating when/where to use move/auto/...Stress of recruiters bugging you with C++ jobs

Page 10: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

C is pretty easy to learn but C++ is clearly complex. The basics are easy but it takes years to become a master in it (although C++11 is making it a bit easier).

Page 11: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component
Page 12: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

very expert

much ninja

so impresswow!

Page 13: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

You may have heard a few things...Forget about 1995C++ is a living languageLanguage tweaks make it easier to do the right thingSpeed is only part of the story; code reuse is vitalVisual Studio is your friend and confidant

Page 14: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Eighty twentyIn C++, a few things done well will get you most of the way to your solution(Or all the way)

Vri

adic

Tem

pla

tes

C+

+ 1

4

Cust

om

allo

cato

rs

Con

tain

ers

an

d

Alg

ori

thm

sR

efe

ren

ces,

Con

sta

nts

an

d R

an

ge-F

or

RTTI

Lam

bda-F

u

Ob

jects

an

d R

AII

Cla

ss intr

icaci

es

Move S

em

anti

cs

Overl

oadin

g n

ew

and d

ele

te

Page 15: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Start with a good book

Good C++ books focus on C++11 and modern C++.

Don’t learn bad habits!

Page 16: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Language EssentialsObjectsRAIIContainers and AlgorithmsReferences, Constants and Range-For

// Thanks to James McNellis!

Page 17: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Language EssentialsObjectsRAIIContainers and AlgorithmsReferences, Constants and Range-For

Page 18: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

What is an Object?void f(){ int x{0};}

Page 19: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Copying and Assignmentvoid f(){ int x{0};

int y{x}; // initialize y to be a copy of x y = x; // assign the value of x to y}

Page 20: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Copying and Assignmentclass MyClass { /* ... */ };

void f(){ MyClass x{};

MyClass y{x}; // initialize y to be a copy of x y = x; // assign the value of x to y}

Page 21: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Four Special Operationsvoid f(){ int x{0}; // Construction

int y{x}; // Copy Construction y = x; // Copy Assignment}// y.~int() // Destruction // x.~int()

Page 22: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Four Special Operationsclass MyClass { /* ... */ };

void f(){ MyClass x; // Construction

MyClass y{x}; // Copy Construction y = x; // Copy Assignment}// y.~MyClass() // Destruction // x.~MyClass()

Page 23: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Four Special Operationsclass MyClass{public: MyClass() { puts("Constructing"); } ~MyClass() { puts("Destroying"); } MyClass(MyClass const&) { puts("Copying"); } void operator=(MyClass const&) { puts("Assigning"); }};

Page 24: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Four Special Operationsclass MyClass { /* previously defined */ };

void f(){ MyClass x; // Constructing

MyClass y{x}; // Copying y = x; // Assigning}// y.~MyClass() // Destroying // x.~MyClass() // Destroying

Page 25: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Each object has an addressclass MyClass { /* ... */ };

void f(){ MyClass x;

MyClass* p{&x}; // p is a pointer to x // *p is another name for x}

Page 26: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Different objects have different addresses

void f(){ MyClass x; MyClass y;

// Different objects have different addresses MyClass* px{&x}; // px points to x MyClass* py{&y}; // py points to y assert(px != py);}

Page 27: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Special null pointer valuevoid f(){ MyClass* p{nullptr};}

Page 28: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Pointers are objects toovoid f(){ MyClass x;

MyClass* px1{&x}; MyClass* px2{px1}; assert(px1 == px2);

px1 = nullptr; assert(px1 != px2);}

Page 29: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Indirection is (mostly) implicit in C#// For value types, x and y are distinctint x = 0;int y = x;

y = 42;

// For reference types, x and y refer to the same objectList<int> x = new List<int>();List<int> y = x;

x.Add(1);

Page 30: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Indirection is explicit in C++// x and *p denote the same objectint x{0};int* p{&x};

// Changes the value of x via *p*p = 42;

Page 31: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Indirection is explicit in C++// x and *p denote the same objectMyClass x{};MyClass* p{&x};

// Changes the value of x via *p*p = get_thing();

Page 32: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Lifetime and Storage DurationEach object has a lifetime…from the point at which the object comes into existence (is constructed)…to the point at which the object goes out of existence (is destructed)

Lifetime is associated with storage durationAutomatic storage durationStatic and thread-local storage durationDynamic storage duration

Page 33: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Automatic Storage Durationvoid f(){ MyClass x{}; // Construction

MyClass y{x}; // Copy Construction y = x; // Copy Assignment

}// y.~MyClass() // Destruction // x.~MyClass() // Destruction

Lifetime of y Lifetime of x

Page 34: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Static Storage DurationMyClass x{}; // Global variable

void f(){ static MyClass y{}; // Function-local static variable}

Page 35: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Dynamic Storage Durationvoid f(){ MyClass* p{new MyClass{}};

delete p;}

Lifetime of *p

Page 36: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Dynamic Storage DurationMyClass* f(){ MyClass* p{new MyClass{}}; return p;}

void g(){ MyClass* p{ f() }; delete p;}

Page 37: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Initializationvoid f(){ int a = 0; // Copy initialization int b(0); // Direct initialization int c{0}; // Brace initialization int d{};}

Page 38: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Initializationint global_int; // Zero-initialized

void f(){ int local_int; // Not initialized}

Page 39: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Initializationvoid f(){ int local_int{}; MyClass local_MyClass{};}

Page 40: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Language EssentialsObjectsRAIIContainers and AlgorithmsReferences, Constants and Range-For

Page 41: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAIIResource Acquisition Is Initialization(Scope-Bound Resource Management)

Page 42: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition Is Initialization)

void f(){ MyClass* p{new MyClass{}};

delete p;}

Page 43: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition Is Initialization)

void f(){ char* const buffer{new char[1024 * 1024]{}};

// ...use the buffer...

delete[] buffer;}

Page 44: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition Is Initialization)

void f(){ char* const buffer{new char[1024 * 1024]{}};

// ... throw std::runtime_error{"oops“}; // ...

delete[] buffer;}

Page 45: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition Is Initialization)

void f(){ char* const buffer{new char[1024 * 1024]{}};

// ... return; // ...

delete[] buffer;}

Page 46: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition Is Initialization)

void f(){ // Not actual C++ char* const buffer{new char[1024 * 1024]{}}; try { // ...use the buffer... } finally { delete[] buffer; }}

Page 47: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition Is Initialization)

void f(){ // Not actual C++ using (char* const buffer{new char[1024 * 1024]{}}) { // ...use the buffer... }}

Page 48: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition is Initialization)

void f(){ scoped_buffer buffer{new char[1024 * 1024]{}};

// ...use the buffer...

}// buffer.~scoped_buffer()

Page 49: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition is Initialization)

struct scoped_buffer{public: scoped_buffer(char* const p) : _p{p} { } ~scoped_buffer() { delete[] _p; } char* get() const { return _p; }

private: scoped_buffer(scoped_buffer const&) = delete; void operator=(scoped_buffer const&) = delete;

char* _p;};

Page 50: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition is Initialization)void f(){ scoped_buffer buffer{new char[1024 * 1024]{}};

// ...use the buffer...

}// buffer.~scoped_buffer()

Page 51: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition is Initialization)

void f(size_t const buffer_size){ std::unique_ptr<char[]> const buffer{new char[buffer_size]{}};

// ...code that uses the buffer...}

Page 52: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition is Initialization)

std::unique_ptr<char[]> f(size_t const buffer_size){ std::unique_ptr<char[]> buffer{new char[buffer_size]{}};

return buffer;}

Page 53: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

C++ Rule #1Never use delete in your codeAlways use a smart pointer to manage dynamic lifetimesunique_ptr, shared_ptr, ComPtr, etc.

Page 54: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition is Initialization)

std::mutex _sync;

void f(){ _sync.lock();

// ...synchronized code...

_sync.unlock();}

Page 55: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition is Initialization)

void f(){ std::unique_lock<std::mutex> lock(_sync);

// ...synchronized code...}

Page 56: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition is Initialization)

void f(char const* const file_name){ FILE* file(fopen(file_name, "r"));

// ...code that uses the file...

fclose(file);}

Page 57: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

RAII (Resource Acquisition is Initialization)

void f(char const* const file_name){ std::ifstream file(file_name);

// ...code that uses the file...}

Page 58: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

C++ Rule #1 (Revised)Never manage resources manually in your C++ codeAlways use RAII container typesUse existing RAII container types where possibleWrite your own when necessary

Page 59: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Language EssentialsObjectsRAIIContainers and AlgorithmsReferences, Constants and Range-For

Page 60: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vectorstd::liststd::setstd::mapstd::unordered_setstd::unordered_map

ListLinkedListSortedSetSortedDictionaryHashSetDictionary

Page 61: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector::insertstd::vector::push_backstd::vector::erasestd::vector::clear

List.InsertList.AddList.RemoveAtList.Clear

Page 62: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };

for (size_t i{0}; i != v.size(); ++i){ std::cout << v[i] << '\n';}

Page 63: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };

for (auto it{v.begin()}; it != v.end(); ++it) // std::vector<int>::iterator{ std::cout << *it << '\n';}

Page 64: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::set<int> s{ 1, 2, 3, 4, 5, 6 };

for (auto it{s.begin()}; it != s.end(); ++it) // std::vector<int>::iterator not now!{ std::cout << *it << '\n';}

Page 65: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::map<int, int> m{ { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 10 }, { 6, 12 }};

for (auto it{m.begin()}; it != m.end(); ++it){ std::cout << it->first << ': ' << it->second << '\n';}

Page 66: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };

for (auto it{v.begin()}; it != v.begin() + 3; ++it){ std::cout << *it << '\n';}

Page 67: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };

v.erase(v.begin() + 3); // Removes the 4v.insert(v.begin() + 2, 10); // Inserts a 10 after the 2

int const x{*(v.begin() + 1)}; // Gets the 2

*(v.begin() + 1) = 9; // Changes the 2 to a 9

int const y{*(v.begin() + 1000)}; // Invalid!

Page 68: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and AlgorithmsOkay, what about operations like sort and find?

v.sort(); // Nope; doesn't existv.find(4); // Nope; doesn't exist

Page 69: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithms

Iterators

Containers

Algorithms

Page 70: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 3, 5, 2, 4, 6 };

std::sort(v.begin(), v.end());

Page 71: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 3, 5, 2, 4, 6 };

auto const it{std::find(v.begin(), v.end(), 2)};if (it != v.end()){ std::cout << "I found a two!\n";}

Page 72: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };

if (std::binary_search(v.begin(), v.end(), 2)){ std::cout << "I found a two!\n";}

Page 73: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsfor_eachtransformall_ofany_offindFind_if

binary_searchlower_boundupper_boundequal_rangesortstable_sort

copy_ifuniquepartitionremoverotateand many more…

Page 74: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };

std::for_each(v.begin(), v.end(), [](int const x){ std::cout << x << '\n';})

Page 75: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };

std::transform(v.begin(), v.end(), v.begin(), [](int const x){ return x * x;});

Page 76: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };std::vector<int> w(v.size());

std::transform(v.begin(), v.end(), w.begin(), [](int const x){ return x * x;});

Page 77: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };std::vector<int> w;

std::transform(v.begin(), v.end(), std::back_inserter(w), [](int const x){ return x * x;});

Page 78: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 3, 5, 2, 4, 6 };

std::sort(v.begin(), v.end(), [](int const lhs, int const rhs){ return lhs > rhs;});

Page 79: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };

bool const all_are_even(std::all_of(v.begin(), v.end(), [](int const x){ return x % 2 == 0;}));

Page 80: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsstd::vector<int> v{ 1, 2, 3, 4, 5, 6 };

std::set<int> s(v.begin(), v.end());

Page 81: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsint a[6]{ 1, 3, 5, 2, 4, 6 };

std::sort(begin(a), end(a));

Page 82: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmsvoid f(int* const data, size_t const count){ int* const first{data}; int* const last {data + count};

std::sort(first, last);}

Page 83: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Containers and Algorithmstemplate <typename Iterator, typename Function>bool all_of(Iterator const first, Iterator const last, Function const f){ for (Iterator it{first}; it != last; ++it) { if (!f(*it)) return false; }

return true;}

Page 84: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Language EssentialsObjectsRAIIContainers and AlgorithmsReferences, Constants and Range-For

Page 85: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Referencesvoid f(){ int x{0}; // x is an object

int& rx{x}; // rx is a reference to x // it is another name for x}

Page 86: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Referencesvoid f(std::vector<int>& v){ // ...Use the vector...}

Page 87: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

constvoid f(){ int const x{0}; x = 42; // Invalid; x is const}

Page 88: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

constvoid f(){ int x(0);

int const& rx(x); int const* px(&x);

x = 42; // Ok, x is not const rx = 42; // Invalid; rx is const *px = 42; // Invalid; *px is const}

Page 89: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

constvoid f(std::vector<int> const& v){ // ...Use the vector but don't modify it...}

Page 90: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Range-forvoid f(){ std::vector<int> v = { 1, 2, 3, 4 };

for (const auto& x : v) { std::cout << x << '\n'; }}

Page 91: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

autostd::vector<int> get_stuff();

void f(){ auto the_stuff = get_stuff();}

Page 92: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

I was once on a project where performance was important, so the core of the program was written in C++. Other people would use it on different platforms including Unix and Linux.

Page 93: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

You need portable codeWhen it comes to portable code, C++ is hard to beat. It is build on a rich heritage of compatibility.There are differences among compilers, but all provide a reasonable ISO C++ standard compliant experience.

Page 94: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

ScenarioCode needs to perform well on multiple devicesCode needs to be accessible on multiple devices

Page 95: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

SymptomsYou need features only accessible to native codePerformance concerns for managed language

Page 96: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Examples<pattern: hamburger, UI layer on top, device API on bottom, meat is the C++ portable layer>

Page 97: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Demo

Portable Code

Page 98: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

GotchasSource and build management complexityConformance differences

Page 99: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

I also use C++ when I need to access some specific features from CryptoAPI or create high performance stuff web server stuff.

If you are planning to develop a math intensive library such as a quant library, stochastic analysis, signal processing or alike, go with C++.

Page 100: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

You need to tune a managed applicationC# performs admirably across a range of applications.But!Sometimes application hot spots drive down performance and cannot be solved in native code alone.

Page 101: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

SymptomsProfiler exposes code that has already been optimizedCustom graphics UI is sluggishExcessive amounts of data marshalling

Page 102: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

ExamplesCrunching large amounts of data (image, audio)Excessive amounts of data marshallingLarge file processingComplex algorithms (financial, data structures)

Page 103: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Demo

C++ AMP

Page 104: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

GotchasPremature optimizationTradeoffs (simplicity/maintainability for complexity)Missed or unforeseen causes (constrained by db)Installation and deployment

Page 105: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

I also use C++ when I need to access some specific features from CryptoAPI or create high performance stuff web server stuff.

If you are planning to develop a math intensive library such as a quant library, stochastic analysis, signal processing or alike, go with C++.

Page 106: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Talking to older systemsOld systems never seem to die. Insurance, engineering, manufacturing, medical – many industries run on older applications. Submitting a claim or new part order may require specialized protocols.

Page 107: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

SymptomsCompany has a place you go to request tape loadsBusiness rules are a “black box”Reliance on bridging middleware

Page 108: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

ExamplesHealth care claims processing and adjudicationEngineering modelling Chemical processing and schedulingHome-grown, stove-pipe systemsJob systems

Page 109: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

GotchasAPIs might be brittle...and pickyPerformance mismatchError handling (return codes)

Page 110: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

You need to use existing code librariesCritical functionality vital to your project exist only in native C/C++ libraries.

Page 111: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

SymptomsSchedule or other pressures make rewrite impossibleSource is inaccessible and/or surrounded by folkloreFeature is already implemented in OSS libraryExisting code is compliant/certified/secureCustom hardware control

Page 112: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

ExamplesEDI transmission componentData cryptography servicesComputer visionAssembly line product quality hardwarePhysics, graphics, or other niche

Page 113: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Demo

Consuming and extending APIs

Page 114: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

GotchasDependency on external development resourcesToo many cross-boundary callsData conversions (ascii / MBCS / Unicode / platform string...)Memory leaks, null pointers, security vulnerabilities

Page 115: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Thank YouPing me any [email protected]

Follow Visual C++ Team Bloghttp://blogs.msdn.com/b/vcblog/

We want your suggestions, help us improve Visual Studio and Visual C++Visit Visual C++ on User Voice http://aka.ms/UserVoiceVisualCPP

Page 116: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Favorite Things for C++ Developers in Visual Studio 2013, DEV-B223

Debugging Tips and Tricks in Visual Studio 2013, DEV-B352

Related content

What's New in Microsoft Visual Studio 2013 for C++ Developers, DEV-H223

Find me later in the Visual Studio booth, on Twitter @visualc or at [email protected]

Page 117: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Resource 1

Track resources

Resource 2

Resource 3

Resource 4

Required Slide*delete this box when your slide is finalized

The PMs will supply the content for this slide, which will be inserted during the final scrub.

Page 118: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Resources

Learning

Microsoft Certification & Training Resources

www.microsoft.com/learning

msdn

Resources for Developers

http://microsoft.com/msdn

TechNet

Resources for IT Professionals

http://microsoft.com/technet

Sessions on Demand

http://channel9.msdn.com/Events/TechEd

Page 119: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Complete an evaluation and enter to win!

Page 120: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

Evaluate this session

Scan this QR code to evaluate this session.

Page 121: C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component

© 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.