27
Modern Native C++ Development for Maximum Productivity Kate Gregory Gregory Consulting www.gregcons.com/kateblog, @gregcons DEV303

Modern Native C++ Development for Maximum Productivity

  • Upload
    tatum

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

DEV303. Modern Native C++ Development for Maximum Productivity. Kate Gregory Gregory Consulting www.gregcons.com/kateblog, @ gregcons. Language and Library updates C++0x and TR1 Lambdas, auto unique_ptr , make_shared IDE improvements Intellisense – no ncb - PowerPoint PPT Presentation

Citation preview

Page 1: Modern Native C++ Development  for  Maximum Productivity

Modern Native C++ Development for Maximum Productivity

Kate GregoryGregory Consultingwww.gregcons.com/kateblog, @gregcons

DEV303

Page 2: Modern Native C++ Development  for  Maximum Productivity

Agenda

Language and Library updatesC++0x and TR1Lambdas, autounique_ptr, make_shared

IDE improvementsIntellisense – no ncbNavigate To , red squiggles

ConcurrencyPPLNew debug windowsConcurrency profiler

What there isn’t time for

MFC UpdatesRibbon designerWindows 7 support

shared_ptr, nullptrRvalue references, move constructors, std::moveMore library additions eg copy_if, is_sorted etc

Page 3: Modern Native C++ Development  for  Maximum Productivity

TR1 and C++0x

TR1 is Technical Report 1, released in 2005C++0x is the upcoming C++ standard

Some of each were added in Visual C++ 2008 SP 1(VC9SP1)

More are now in Visual C++ 2010(VC10)

Page 4: Modern Native C++ Development  for  Maximum Productivity

Lambdas for C++

What’s a Lambda?Lambda expression or lambda function: an expression that specifies an anonymous function objectImagine handing an operation or function (code) to some other operation or function

For generic workFor a functional styleFor concurrencyFor readability

Eliminate tiny functions

Page 5: Modern Native C++ Development  for  Maximum Productivity

Tiny Functions

void print_square(int i) {

cout << i*i << endl;}

int main() {

vector<int> v;for_each(v.begin(), v.end(), print_square);

}

Page 6: Modern Native C++ Development  for  Maximum Productivity

Why Does It Need a Name?

int main() { vector<int> v; for_each(v.begin(), v.end(), [](int i) { cout << i*i << endl; } );}

Page 7: Modern Native C++ Development  for  Maximum Productivity

demo

Lambdas

Page 8: Modern Native C++ Development  for  Maximum Productivity

Lambdas That Return Somethingvector<int> v;deque<int> d;transform(v.begin(), v.end(), front_inserter(d), [](int n) { return n * n * n; });transform(v.begin(), v.end(), front_inserter(d), [](int n) -> double {        if (n % 2 == 0) {return n * n * n;} else {return n / 2.0;}    });

Page 9: Modern Native C++ Development  for  Maximum Productivity

Using Variables from Local Scopev.erase(remove_if(v.begin(), v.end(), [x, y](int n) { return x < n && n < y; }),v.end());v.erase(remove_if(v.begin(), v.end(), [=](int n) { return x < n && n < y; }), v.end());for_each(v.begin(), v.end(), [&x, &y](int& r) {        const int old = r;        r *= 2;        x = y;        y = old;     });

Page 10: Modern Native C++ Development  for  Maximum Productivity

Auto

Automatic type deductionauto x = new HugeObject(42);

No more gnarly iterator declarationsfor (auto it = v.begin(); it != v.end(); ++it)

Powered by template argument deduction rulesconst auto* p = new foo and const auto& r = bar work

Page 11: Modern Native C++ Development  for  Maximum Productivity

C++0x Standard Library in VC 2010

Rvalue referencesvector reallocation, etc. exploits move semanticsPerfect forwarding: make_shared<T>(), etc.Std::moveunique_ptr

New member functions: cbegin(), cend(), etc.New algorithms: copy_if(), is_sorted(), etc.Code conversions: <codecvt>Exception propagation: exception_ptrDiagnostics: <system_error>

Page 12: Modern Native C++ Development  for  Maximum Productivity

Smart pointers

shared_ptrArrived in VC9 SP1In VC10: make_shared

unique_ptrLike a shared_ptr without the sharing

Page 13: Modern Native C++ Development  for  Maximum Productivity

make_shared<T>()

VC9 SP1:shared_ptr<T> sp(new T(args));shared_ptr<T> sp(new T(args), del, alloc);

VC10:auto sp = make_shared<T>(args);auto sp = allocate_shared<T>(alloc, args);

13

Page 14: Modern Native C++ Development  for  Maximum Productivity

unique_ptr

Supersedes auto_ptr, which is now deprecatedLightweight and performant

No reference counting overheadNoncopyable but movableWorks just fine in containers

14

Page 15: Modern Native C++ Development  for  Maximum Productivity

Const iterators: cbegin and cendvector<int> v;

for (auto i = v.begin(); i != v.end(); ++i) { // i is vector<int>::iterator}

for (auto i = v.cbegin(); i != v.cend(); ++i) { // i is vector<int>::const_iterator}

Page 16: Modern Native C++ Development  for  Maximum Productivity

Visual Studio 2010 Architecture Changes

Intellisense decoupled from navigationNo need to reparse entire solution after header change

No more .ncb file – SQL CE store insteadMuch quicker to insert/update single symbol

Intellisense faster even in larger solutionsAfter a small code changeSwitching build (e.g., debug to release)

Page 17: Modern Native C++ Development  for  Maximum Productivity

Visual Studio 2010 New Features

Navigate ToFind a symbol

Red SquigglesWithout a build

Call HierarchyCalls FromCalls ToReplaces Call Browser

Page 18: Modern Native C++ Development  for  Maximum Productivity

Native Concurrency Stack

Parallel Patterns Library

parallel_forparallel_for_each

Concurrent Algorithmsparallel_accumulateparallel_partialsum

parallel_invoke…

Threads + UMS

Messaging Primitivessend, receive

asend, try_receivemessage buffers

Concurrency RuntimeSchedulers with

Work-Stealing Queues chores … …

Concurrency Primitivestask handlestask groups

futuressynchronization types

Concurrent Collectionsconcurrent_queueconcurrent_vector

concurrent_hash_map…

chores …

C/C++ Application or Library

Resource Manager

Proc 1 … Proc p

Page 19: Modern Native C++ Development  for  Maximum Productivity

Concurrency and Debugging

Page 20: Modern Native C++ Development  for  Maximum Productivity

demo

Concurrency

Page 21: Modern Native C++ Development  for  Maximum Productivity

C++ Is Very Much Alive

Native code is still a fully supported way of lifeInterop is dramatically easier from C++Templates offer power no other language can match

For both native-only and interop developmentMicrosoft is committed to C++

IDE improvementsMFC improvementsLanguage-level improvements

Page 22: Modern Native C++ Development  for  Maximum Productivity

DEV Track Resources

http://www.microsoft.com/visualstudio http://www.microsoft.com/visualstudio/en-us/lightswitch http://www.microsoft.com/expression/http://blogs.msdn.com/b/somasegar/http://blogs.msdn.com/b/bharry/http://www.microsoft.com/sqlserver/en/us/default.aspxhttp://www.facebook.com/visualstudio

Page 23: Modern Native C++ Development  for  Maximum Productivity

Resources

www.microsoft.com/teched

Sessions On-Demand & Community Microsoft Certification & Training Resources

Resources for IT Professionals Resources for Developers

www.microsoft.com/learning

http://microsoft.com/technet http://microsoft.com/msdn

Learning

http://northamerica.msteched.com

Connect. Share. Discuss.

Page 24: Modern Native C++ Development  for  Maximum Productivity

Complete an evaluation on CommNet and enter to win!

Page 25: Modern Native C++ Development  for  Maximum Productivity

Scan the Tag to evaluate this session now on myTech•Ed Mobile

Page 26: Modern Native C++ Development  for  Maximum Productivity
Page 27: Modern Native C++ Development  for  Maximum Productivity