Useful C++ Features You Should be Using

35
© 2016 Embarcadero Technologies, Inc. All rights reserved. USEFUL C++ FEATURES YOU SHOULD BE USING David Millington [email protected]

Transcript of Useful C++ Features You Should be Using

Page 1: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

USEFUL C++ FEATURES YOU SHOULD BE USINGDavid [email protected]

Page 2: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

CONTENT• Type inference• Lambdas• New containers• Variadic templates• Smart pointers• And others

Not a list of C++11 , nor going into every detail - but showing advantages. Useful even if you already use some of these (eg pre-C++11 smart pointers.)

Page 3: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCENot just auto… but let’s start with it.

auto i = 5; // i is an intauto j = foo(); // j is whatever foo() returns

Page 4: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCENot just auto… but let’s start with it.

auto i = 5; // i is an intauto j = foo(); // j is whatever foo() returns

Decltype too:decltype(i) k; // k is i’s type, an intdecltype(i + 5.5f) k; // k is the type of “i + 5.5f”, which is float

Page 5: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCE IN A SIMPLE TEMPLATE

template<typename A, typename B>void foo(A a, B b) { UNKNOWN m = a * b; // int? float? string?}

Page 6: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCE IN A SIMPLE TEMPLATE

template<typename A, typename B>void foo(A a, B b) { auto m = a * b; // Solved.}

Page 7: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCE WHEN USING THE STLSTL provides standard iterators making algorithms applicable regardless of the container type – eg std::find over a std::map vs std::unordered_map.

Fine for templates – but can you take advantage of this yourself in normal, non-templated code?

Page 8: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCE WHEN USING THE STLstd::map<int, std::wstring> items;

for (std::map<int, std::wstring>::const_iterator it = items.begin(); it != items.end(); it++){}

Page 9: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCE WHEN USING THE STLstd::unordered_map<int, std::wstring> items;

for (std::map<int, std::wstring>::const_iterator it = items.begin(); it != items.end(); it++){}

Won’t compile, because despite the interface (begin, end) being the same, the iterator types are different.

Page 10: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCE WHEN USING THE STLstd::unordered_map<int, std::wstring> items;

for (std::unordered_map<int, std::wstring>::const_iterator it = items.begin(); it != items.end(); it++){}

What a mess!

Page 11: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCE WHEN USING THE STLstd::map<int, std::wstring> items;

for (auto it = items.begin(); it != items.end(); it++){}

std::unordered_map<int, std::wstring> items;Works with either declaration…

Page 12: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCE WHEN USING THE STLstd::unordered_map<int, std::wstring> items;

for (auto it : items){}

Works regardless of the type of itemsMuch shorter, clearer, easier to type code

Page 13: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCE WHEN USING THE STLfor (std::map<int, std::wstring>::const_iterator it = items.begin(); it != items.end(); it++){}

…versus…for (auto it : items){}

Amazing.

Page 14: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

TYPE INFERENCEAuto:• Reduces typing• Remains strongly (and predictably) typed• Useful when the type isn’t known when writing, but can be deduced

at compile time• Allows easy changes• More compact and clear code

Investigate decltype too, especially for templates.

Page 15: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

LAMBDASstd::list<int> items { 1, 2, 3 };…std::replace_if(items.begin(), items.end(), ???, 99);

Page 16: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

LAMBDASstruct ItemComparer { private:

int m_Value;public:

ItemComparer(const int n) : m_Value(n) {};bool operator()(int i) const

{ return i % m_Value == 0; }};

std::replace_if(items.begin(), items.end(), ItemComparer(n), 99);

Page 17: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

LAMBDASint n = 2;

std::replace_if(items.begin(), items.end(), [n](int& i) { return i % n == 0; }, 99);

Page 18: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

LAMBDAS[] The variable or capture list () The argument list {} Code

Making the simplest lambda [](){}a method that captures nothing, is passed no arguments, and does nothing.

[n](int& i) { return i % n == 0; }

Page 19: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

LAMBDASauto f = [this, n, &j](int& i) { return i % n == 0; };

• Neat and readable• Inline in code• Inlineable and performant• Capture variables by value or reference• Behave like an object

Page 20: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

VARIADIC TEMPLATEStemplate<typename T>T mult(T first) { return first;}

template<typename T, typename... Others>T mult(T first, Others... others) { return first * mult(others...);}

int z = mult(1, 2, 3, 4); // z = 24* Based on http://eli.thegreenplace.net/2014/variadic-templates-in-c/

Page 21: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

VARIADIC TEMPLATES

std::tuple<int, float, std::string> t { 1, 2.5, "hello"};

float v = std::get<1>(t);

Page 22: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

SMART POINTERSNo:

Foo* foo = new Foo();__try { //} __finally { delete foo;}

Yes (sortof):

{ scoped_ptr<Foo> foo = new Foo();}^- foo is freed

Page 23: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

SMART POINTERSUsing boost or C++11:

{ shared_ptr<Foo> foo = new Foo(); bar->myfoo = foo; // bar::myfoo is a shared_ptr<Foo>} // <- foo is not freed, a reference is still held

Page 24: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

SMART POINTERSUsing boost? Move to shared_ptr (very familiar!) and unique_ptr instead of scoped_ptr

In general, already using them or not:• Use unique_ptr where possible• Use shared_ptr not out of ease but when you want to share

• Don’t expect automatic threadsafety apart from +/- refcount• Use shared_ptr<Foo> p = make_shared<Foo>; and

make_unique<T> if available - http://stackoverflow.com/questions/17902405/how-to-implement-make-unique-function-in-c11

• Never write new or delete

Page 25: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

CONTAINERShash_map – common, but never standardised!

Use unordered_set, unordered_map, unordered_multiset, unordered_multimap.

Page 26: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

ATOMIC PRIMITIVESC++11 defines a memory model – so race conditions and odd threading behaviour no longer undefined behaviour.

Low-level primitives in <atomic>

Generally stick to things like mutex.

Page 27: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

STATIC ASSERTIONSIn the past, clever code & macros to get something like: STATIC_ASSERT(x);

Now, static_assert(sizeof(float) == 4, "float has unexpected size");

Page 28: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

C++BUILDER• FireMonkey –

cross-platform UI, native controls, multi-device specialization

• Similar for other frameworks – database, REST, enterprise, RAD Server…

Page 29: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

C++BUILDERCompilers:• Classic BCC32• New Clang-enhanced

Resources:• Docwiki lists C++11 features in

‘old’ classic compiler• And language support in new

Clang compiler (including newer than C++11 features)

Page 30: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

USEFUL FEATURESCode clarity• Auto – clearer, easier• Lambdas – inline, readable, function

content visible where it’s used

Safety• Smart pointers – use unique_ptr where

possible• Static assertions

Misc• Standardised unordered containers

• Standardised memory model – defined (even if unexpected) behaviour

C++Builder• Cross-platform support• UI – specializable for different platforms

and devices. Yet you can use native controls too.

• New compilers – Clang-based, plus our extensions. Better language support, better performance. Free compiler available!

Page 31: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

USEFUL FEATURESCode clarity• Auto – clearer, easier• Lambdas – inline, readable, function

content visible where it’s used

Safety• Smart pointers – use unique_ptr where

possible• Static assertions

Misc• Standardised unordered containers

• Standardised memory model – defined (even if unexpected) behaviour

C++Builder• Cross-platform support• UI – specializable for different platforms

and devices. Yet you can use native controls too.

• New compilers – Clang-based, plus our extensions. Better language support, better performance. Free compiler available!

Page 32: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

USEFUL FEATURESCode clarity• Auto – clearer, easier• Lambdas – inline, readable, function

content visible where it’s used

Safety• Smart pointers – use unique_ptr where

possible• Static assertions

Misc• Standardised unordered containers

• Standardised memory model – defined (even if unexpected) behaviour

C++Builder• Cross-platform support• UI – specializable for different platforms

and devices. Yet you can use native controls too.

• New compilers – Clang-based, plus our extensions. Better language support, better performance. Free compiler available!

Page 33: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

USEFUL FEATURESCode clarity• Auto – clearer, easier• Lambdas – inline, readable, function

content visible where it’s used

Safety• Smart pointers – use unique_ptr where

possible• Static assertions

Misc• Standardised unordered containers

• Standardised memory model – defined (even if unexpected) behaviour

C++Builder• Cross-platform support• UI – specializable for different platforms

and devices. Yet you can use native controls too.

• New compilers – Clang-based, plus our extensions. Better language support, better performance. Free compiler available!

Page 34: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

USEFUL FEATURESCode clarity• Auto – clearer, easier• Lambdas – inline, readable, function

content visible where it’s used

Safety• Smart pointers – use unique_ptr where

possible• Static assertions

Misc• Standardised unordered containers

• Standardised memory model – defined (even if unexpected) behaviour

C++Builder• Cross-platform support• UI – specializable for different platforms

and devices. Yet you can use native controls too.

• New compilers – Clang-based, plus our extensions. Better language support, better performance. Free compiler available!

Page 35: Useful C++ Features You Should be Using

© 2016 Embarcadero Technologies, Inc. All rights reserved.

RESOURCES• Auto

• http://thbecker.net/articles/auto_and_decltype/section_01.html• Lambdas

• http://www.drdobbs.com/cpp/lambdas-in-c11/240168241• Smart pointers

• http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/• Variadic templates

• http://eli.thegreenplace.net/2014/variadic-templates-in-c/• https://www.murrayc.com/permalink/2015/12/05/modern-c-variadic-template-parameters-and-tuples/

• Static assertions• http://stackoverflow.com/questions/1647895/what-does-static-assert-do-and-what-would-you-use-it-for

• C++ Builder• https://www.embarcadero.com/products/cbuilder • https://www.embarcadero.com/free-tools/ccompiler• http://docwiki.embarcadero.com/RADStudio/Berlin/en/C++11_Language_Features_Compliance_Status• http://

docwiki.embarcadero.com/RADStudio/Berlin/en/C%2B%2B11_Features_Supported_by_RAD_Studio_Clang-enhanced_C%2B%2B_Compilers