Contenu connexe Similaire à Useful C++ Features You Should be Using (20) Plus de Embarcadero Technologies (20) Useful C++ Features You Should be Using1. © 2016 Embarcadero Technologies, Inc. All rights reserved.
USEFUL C++ FEATURES YOU SHOULD BE USING
David Millington
david.millington@embarcadero.com
2. © 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.)
3. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE
Not just auto… but let’s start with it.
auto i = 5; // i is an int
auto j = foo(); // j is whatever foo() returns
4. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE
Not just auto… but let’s start with it.
auto i = 5; // i is an int
auto j = foo(); // j is whatever foo() returns
Decltype too:
decltype(i) k; // k is i’s type, an int
decltype(i + 5.5f) k; // k is the type of
“i + 5.5f”, which is float
5. © 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?
}
6. © 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.
}
7. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE WHEN USING THE STL
STL 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?
8. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE WHEN USING THE STL
std::map<int, std::wstring> items;
for (std::map<int, std::wstring>::const_iterator
it = items.begin(); it != items.end(); it++)
{
}
9. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE WHEN USING THE STL
std::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.
10. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE WHEN USING THE STL
std::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!
11. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE WHEN USING THE STL
std::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…
12. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE WHEN USING THE STL
std::unordered_map<int, std::wstring> items;
for (auto it : items)
{
}
Works regardless of the type of items
Much shorter, clearer, easier to type code
13. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE WHEN USING THE STL
for (std::map<int, std::wstring>::const_iterator
it = items.begin(); it != items.end(); it++)
{
}
…versus…
for (auto it : items)
{
}
Amazing.
14. © 2016 Embarcadero Technologies, Inc. All rights reserved.
TYPE INFERENCE
Auto:
• 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.
15. © 2016 Embarcadero Technologies, Inc. All rights reserved.
LAMBDAS
std::list<int> items { 1, 2, 3 };
…
std::replace_if(items.begin(), items.end(),
???, 99);
16. © 2016 Embarcadero Technologies, Inc. All rights reserved.
LAMBDAS
struct 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);
17. © 2016 Embarcadero Technologies, Inc. All rights reserved.
LAMBDAS
int n = 2;
std::replace_if(items.begin(), items.end(),
[n](int& i) { return i % n == 0; },
99);
18. © 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; }
19. © 2016 Embarcadero Technologies, Inc. All rights reserved.
LAMBDAS
auto 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
20. © 2016 Embarcadero Technologies, Inc. All rights reserved.
VARIADIC TEMPLATES
template<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/
21. © 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);
22. © 2016 Embarcadero Technologies, Inc. All rights reserved.
SMART POINTERS
No:
Foo* foo = new Foo();
__try {
//
} __finally {
delete foo;
}
Yes (sortof):
{
scoped_ptr<Foo> foo =
new Foo();
}
^- foo is freed
23. © 2016 Embarcadero Technologies, Inc. All rights reserved.
SMART POINTERS
Using 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
24. © 2016 Embarcadero Technologies, Inc. All rights reserved.
SMART POINTERS
Using 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
25. © 2016 Embarcadero Technologies, Inc. All rights reserved.
CONTAINERS
hash_map – common, but never standardised!
Use unordered_set, unordered_map, unordered_multiset,
unordered_multimap.
26. © 2016 Embarcadero Technologies, Inc. All rights reserved.
ATOMIC PRIMITIVES
C++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.
27. © 2016 Embarcadero Technologies, Inc. All rights reserved.
STATIC ASSERTIONS
In the past, clever code & macros to get something like:
STATIC_ASSERT(x);
Now,
static_assert(sizeof(float) == 4,
"float has unexpected size");
28. © 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…
29. © 2016 Embarcadero Technologies, Inc. All rights reserved.
C++BUILDER
Compilers:
• 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)
30. © 2016 Embarcadero Technologies, Inc. All rights reserved.
USEFUL FEATURES
Code 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!
31. © 2016 Embarcadero Technologies, Inc. All rights reserved.
USEFUL FEATURES
Code 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!
32. © 2016 Embarcadero Technologies, Inc. All rights reserved.
USEFUL FEATURES
Code 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!
33. © 2016 Embarcadero Technologies, Inc. All rights reserved.
USEFUL FEATURES
Code 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!
34. © 2016 Embarcadero Technologies, Inc. All rights reserved.
USEFUL FEATURES
Code 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!
35. © 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