useful c++ features you should be using

Post on 16-Jan-2017

187 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

© 2016 Embarcadero Technologies, Inc. All rights reserved.

USEFUL C++ FEATURES YOU SHOULD BE USINGDavid Millingtondavid.millington@embarcadero.com

© 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.)

© 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

© 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

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

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

© 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?

© 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++){}

© 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.

© 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!

© 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…

© 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

© 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.

© 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.

© 2016 Embarcadero Technologies, Inc. All rights reserved.

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

© 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);

© 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);

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

© 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

© 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/

© 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);

© 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

© 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

© 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

© 2016 Embarcadero Technologies, Inc. All rights reserved.

CONTAINERShash_map – common, but never standardised!

Use unordered_set, unordered_map, unordered_multiset, unordered_multimap.

© 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.

© 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");

© 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…

© 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)

© 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!

© 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!

© 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!

© 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!

© 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!

© 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

top related