SlideShare une entreprise Scribd logo
1  sur  115
@Uilian Ries
02 / 2016
https://goo.gl/U0fZJa
The Elements of C++11
AGENDA
•Why should I learn about C++11?
•C++11 Features
•deduction of a type from an initializer
•the type of an expression
•Initializer-list and uniform initialization
•rvalue references and move semantics
•static assertions
•preventing exception propagation
•override controls
•scoped and strongly typed enums
•lambda expressions
•defaulted and deleted functions
•smart pointers
•standard container array
WHY SHOULD I LEARN ABOUT C++11?
fonte: https://isocpp.org/files/img/wg21-timeline.png
WHY SHOULD I LEARN ABOUT C++11?
“C++11 feels like a new language” - Bjarne Stroustrup
DEDUCTION OF A TYPE FROM AN INITIALIZER
// C++98
std::map<std::string, std::vector<int, int> >::iterator it = foo.begin();
Singleton& singleton = Singleton::instance();
int bar;
// C++11
auto it = std::begin(foo);
auto& singleton = Singleton::instance();
auto bar; // error: declaration has no initializer
auto bar = 0;
DEDUCTION OF A TYPE FROM AN INITIALIZER
// C++98
std::vector<std::string>::const_iterator it;
for (it = strings.begin(); it != strings.end(); ++it) { /*...*/ }
// C++11
for (auto it : strings) { /*...*/ }
DEDUCTION OF A TYPE FROM AN INITIALIZER
// C++11
auto sum(int a, int b) -> int
{
return a + b;
}
template<typename Builder>
void buildAndProcess(Builder& builder)
{
auto baz = builder.Build();
baz.work();
}
DEDUCTION OF A TYPE FROM AN INITIALIZER
// C++11
template<typename Builder>
auto buildAndProcess(Builder& builder) -> ???
{
auto baz = builder.Build();
baz.work();
return baz;
}
THE TYPE OF AN EXPRESSION
•decltype
•Auto lets you declare a variable with a particular type
• decltype lets you extract the type from a variable (or any other
expression)
THE TYPE OF AN EXPRESSION
// C++11
int bar = 42;
decltype(bar) qux = bar;
auto qux = bar; // same
int qux();
decltype(qux()); // int
DEDUCTION OF A TYPE FROM AN INITIALIZER
// C++11
template<typename Builder>
auto buildAndProcess(Builder& builder) -> decltype( builder.Build() )
{
auto baz = builder.Build();
baz.work();
return baz;
}
// C++14
template<typename Builder>
auto buildAndProcess(Builder& builder)
{
auto baz = builder.Build();
baz.work();
return baz;
}
DEDUCTION OF A TYPE FROM AN INITIALIZER
#include <iostream>
struct Foo {
Foo () { std::cout << 1; }
~Foo() { std::cout << 3; }
void print() { std::cout << 2; }
};
int main() {
Foo foo[3];
for (auto it : foo) { it.print(); }
return 0;
}
fonte: http://ideone.com/ux5CeG for (auto& it : foo) { it.print(); }
DEDUCTION OF A TYPE FROM AN INITIALIZER
// C++11
int& bar();
auto foo = bar(); // int or int& ?
/* auto defaults to being by-value for references */
auto& foo = bar(); // int&
int* foo = new int(5);
auto bar = foo; // bar is int*
auto* qux = foo; // as before
DEDUCTION OF A TYPE FROM AN INITIALIZER
std::string foo("foo");
unsigned size = foo.size(); // type shortcut
std::string::size_type size = foo.size(); // official return type
auto size = foo.size(); // correct deduction
DEDUCTION OF A TYPE FROM AN INITIALIZER
// C++11
#include <unordered_map>
std::unordered_map<std::string, std::string> bar;
// bar receives some elements ...
for (const std::pair<std::string, std::string>& it : bar) { /* ... */ }
for (const auto& it : bar) { /* ... */ }
DEDUCTION OF A TYPE FROM AN INITIALIZER
•Things to remember:
•auto must be initialized;
•the compiler can evaluates better than you;
•auto can simplify your expression;
•a function can return auto type;
•a function parameter can be auto type (C++14);
•auto defaults to being by-value for references.
INITIALIZER-LIST AND UNIFORM INITIALIZATION
// C++98
int foo[] = {16,2,77,29};
std::vector<int> bar(foo, foo + sizeof(foo) / sizeof(foo[0]));
// C++98
std::map<int, bool> qux;
qux[100] = true;
qux[110] = false;
// C++11
std::vector<int> bar = {16,2,77,29};
// C++11
std::map<int, bool> qux = {{100, true}, {110, false}};
INITIALIZER-LIST AND UNIFORM INITIALIZATION
#include <iostream>
#include <initializer_list>
struct Foo {
Foo(int a, int b) { std::cout << a << " " << b << std::endl;
}
Foo (std::initializer_list<int> list) {
for (auto it : list) { std::cout << it << std::endl; }
}
};
int main() {
Foo foo{1, 2};
Foo foobar(1, 2);
return 0;
}
INITIALIZER-LIST AND UNIFORM INITIALIZATION
// C++98
int foo = 5.2; // narrowing convertion
int bar(3.14); // narrowing convertion
// C++11
int foo{5.2}; // error: narrowing convertion
int qux[3]{1, 2, 3};
std::vector<char> baz{'a', 'b', 'c'};
// C++98
Baz::Baz(int bar, int corge) : bar_(bar), corge_(corge) {}
insert_vlan( vlan(100, TAGGED) );
// C++11
Baz::Baz(int bar, int corge) : bar_{bar}, corge_{corge} {}
insert_vlan( {100, TAGGED} );
INITIALIZER-LIST AND UNIFORM INITIALIZATION
•initializer-list
•array of objects of type const T;
•must be homogeneous;
•immutable sequence.
std::initializer_list<int> qux{1, 2, 3, 4, 5}; // Okay
std::initializer_list<int> qux{1, 2, 3.0, 4, 5}; // NOK!
error: narrowing conversion of '3.0e+0' from 'double' to 'int'
inside { } [-Wnarrowing]
std::initializer_list<int> qux{1, 2, 3, 4, 5};
*qux.begin() = 42;
error: assignment of read-only location
INITIALIZER-LIST AND UNIFORM INITIALIZATION
#include <iostream>
#include <type_traits>
int main() {
auto foo{5};
auto bar = {3, 5};
auto qux{4, 2};
std::cout << std::is_integral<decltype(foo)>::value;
std::cout << std::is_integral<decltype(bat)>::value;
std::cout << std::is_integral<decltype(qux)>::value;
return 0;
}
fonte: http://ideone.com/QMhxvv
auto foo = 5;
error: direct-list-initialization of 'auto'
requires exactly one element [-
fpermissive] auto qux{4, 2};
RVALUE REFERENCES AND MOVE SEMANTICS
•In C++11, there are rvalues and lvalues:
•lvalues is an expression that could be the left hand side of an
assignment;
•rvalues is an expression that could be the right hand side of an
assignment;
int foo = 42; // foo is a lvalue; 42 is an rvalue
RVALUE REFERENCES AND MOVE SEMANTICS
•References:
•lvalue reference is formed by placing an & after some type;
•rvalue reference is formed by placing an && after some type;
int bar = 42; // bar is a lvalue
int& bar_ref = bar; // bar_ref is a lvalue reference
int& baz = 42; // error: can't bind rvalue
const int& baz = 42; // okay! baz is const reference
int&& qux = 42; // qux is an rvalue reference
qux = 54; // okay! can replace
int&& corge = bar; // error: can't bind lvalue
RVALUE REFERENCES AND MOVE SEMANTICS
•Why would we want to do this?:
•move semantics;
•perfect forward;
RVALUE REFERENCES AND MOVE SEMANTICS
•Move semantics
•replaces expansive copy with less expansive move;
•enables the creation of move-only type as unique_ptr;
•Consider:
template <class T>
swap(T& a, T& b)
{
T tmp(a); // now we have two copies of a
a = b; // now we have two copies of b
b = tmp; // now we have two copies of tmp (aka a)
}
RVALUE REFERENCES AND MOVE SEMANTICS
#include <utility>
template <class T>
swap(T& a, T& b)
{
T tmp(std::move(a)); // move a contents to tmp; a is empty
a = std::move(b); // move b contents to a; b is empty
b = std::move(tmp); // move tmp contents to b; tmp is empty
}
•move gives its target the value of its argument, but is not
obliged to preserve the value of its source;
•if move is not possible, then the value will be copied.
RVALUE REFERENCES AND MOVE SEMANTICS
class Foo { // Temporary
Object
std::string bar;
public:
Foo(const std::string& ss) : bar(ss) {
assert(!bar.empty()); // ss was copied!
}
};
int main() {
std::string baz("F00B4R");
Foo foo(baz);
assert(!baz.empty()); // baz is no more necessary!
return 0;
}
http://ideone.com/fzZHCB
RVALUE REFERENCES AND MOVE SEMANTICS
#include <utility> // Temporary Object
class Foo {
std::string bar;
public:
Foo(std::string&& ss) : bar(std::move(ss)) {
assert(!bar.empty()); // ss was moved!
}
};
int main() {
std::string baz("F00B4R");
Foo foo(std::move(baz));
assert(baz.empty()); // baz was moved!
return 0;
}
http://ideone.com/fzZHCB
RVALUE REFERENCES AND MOVE SEMANTICS
•About move and forward functions
•move doesn’t move;
•forward doesn’t forward;
•neither does anything at runtime;
•generate nothing.
RVALUE REFERENCES AND MOVE SEMANTICS
•About move and forward functions
•move and forward are functions that performs casts;
•move is a unconditional cast to an rvalue;
•forward performs this cast only if a particular condition is fulfilled.
template<typename T>
typename remove_reference<T>::type&&
move(T&& param)
{
using ReturnType = typename remove_reference<T>::type&&;
return static_cast<ReturnType>(param);
}
•move returns a && type (rvalue reference);
•remove_reference ensures that && is applied only to non-reference.
RVALUE REFERENCES AND MOVE SEMANTICS
•Perfect Forward
•write function templates that take arbitrary arguments;
•forward exactly the same arguments as were passed.
•Consider:
template <class T>
std::shared_ptr<T>
factory()
{
return std::make_shared<T>();
}
RVALUE REFERENCES AND MOVE SEMANTICS
template <class T, class Arg>
std::shared_ptr<T>
factory(Arg& arg)
{
return std::make_shared<T>(arg);
}
int main() {
std::string ss("FOO");
auto foo = factory<std::string>(ss);
auto bar = factory<std::string>(std::move(ss)); // error!
return 0;
}
RVALUE REFERENCES AND MOVE SEMANTICS
template <class T, class Args>
std::shared_ptr<T>
factory(Args&& args)
{
return std::make_shared<T>(std::forward<Args>(args));
}
•forward preserves the lvalue/rvalue-ness of the argument that
was passed to factory;
•Args&& can be anything.
RVALUE REFERENCES AND MOVE SEMANTICS
•Things to remember
•move can be cheaper than copy;
•std::move doesn’t move; it’s an unconditional cast to an rvalue;
•std::forward doesn’t forward; it’s casts its argument to an rvalue only if
that argument is bound to an rvalue;
RVALUE REFERENCES AND MOVE SEMANTICS
•Universal References
•sometimes && is not a rvalue reference.
class Foo{};
// ...
Foo&& foo = Foo(); // && means rvalue reference
auto&& bar = foo; // && doesn't mean rvalue reference
template<typename T>
void process(std::vector<T>&& args); // && means rvalue reference
template<typename T>
void process(T&& args); // && doesn't mean rvalue reference
RVALUE REFERENCES AND MOVE SEMANTICS
•Universal References
•can binds rvalue reference;
•can binds lvalue reference;
•can binds const or non-consts objects.
class Foo{};
// ...
Foo&& foo = Foo(); // we can obtain the address of foo
auto&& bar = foo; // so, bar is a reference to a lvalue
Foo& bar = foo; // same
RVALUE REFERENCES AND MOVE SEMANTICS
•Universal References
template<typename T>
void process(T&& args) {}
process(10); // 10 is a rvalue; T&& is a rvalue reference
Foo foo;
process(foo); // foo is a lvalue; T&& is a lvalue reference, as Foo&
template <typename T>
void foobar(const T&& param) {} // const qualifier disables uref
RVALUE REFERENCES AND MOVE SEMANTICS
•Universal References
•use move on rvalue references and forward on universal references;
class Foo {
std::string bar;
public:
template <typename T>
void set_bar(T&& t) { bar = std::move(t); }
};
int main() {
std::string baz{"F00B4R"};
Foo foo;
foo.set_bar(baz);
assert(!baz.empty()); // Ops!
return 0;
}
http://ideone.com/MXdPXx
RVALUE REFERENCES AND MOVE SEMANTICS
•Universal References
•use move on rvalue references and forward on universal references;
class Foo {
std::string bar;
public:
void set_bar(std::string&& ss) { bar = std::move(ss); }
};
int main() {
std::string baz{"F00B4R"};
Foo foo;
foo.set_bar(baz); // error: no mathing function
assert(!baz.empty());
return 0;
}
http://ideone.com/WFlcon
RVALUE REFERENCES AND MOVE SEMANTICS
•Things to remember: Universal References
•auto&& and T&& for a deduces type T, are universal reference;
•universal references are lvalue references when initialized with
lvalues;
•universal references are rvalue references when initialized with
rvalues;
•use move on rvalue references;
•use forward on universal references.
RVALUE REFERENCES AND MOVE SEMANTICS
•Consider pass by value when cheaper: Overloading
class Foo {
std::vector<std::string> foobar;
public:
void insert(const std::string& ss) {
foobar.push_back(ss); // Copy!
}
void insert(std::string&& ss) {
foobar.push_back(std::move(ss));
}
};
int main() {
Foo foo;
std::string bar("B4R");
foo.insert(bar); // insert by lvalue
foo.insert("F00"); // insert by rvalue
return 0;
} // http://ideone.com/Qb3ASJ
RVALUE REFERENCES AND MOVE SEMANTICS
•Consider pass by value when cheaper: Universal Reference
class Foo {
std::vector<std::string> foobar;
public:
template <typename T>
void insert(T&& ss) {
foobar.push_back(std::forward<T>(ss));
}
};
int main() {
Foo foo;
std::string bar("B4R");
foo.insert(bar); // insert by lvalue
foo.insert("F00"); // insert by rvalue
return 0;
}
http://ideone.com/EPmSho
RVALUE REFERENCES AND MOVE SEMANTICS
•Consider pass by value when cheaper: Passing by value
class Foo {
std::vector<std::string> foobar;
public:
void insert(std::string ss) {
foobar.push_back(std::move(ss));
}
};
int main() {
Foo foo;
std::string bar("B4R");
foo.insert(bar); // insert by lvalue
foo.insert("F00"); // insert by rvalue
return 0;
}
http://ideone.com/c1Hsv7
RVALUE REFERENCES AND MOVE SEMANTICS
•Overloading
•one copy for lvalues;
•one move for rvalues.
•Universal Reference
•one copy for lvalues;
•one move for rvalues.
•Passing by value
•one copy + one move for lvalues;
•two moves for rvalues;
+ parameters that are always copied;
+ nearly efficient as pass by reference;
+ easier to implement;
+ can generate less object code.
STATIC ASSERTIONS
● static_assert performs compile time assertion checking.
static_assert ( bool_constexpr , message );
#include <type_traits>
int main() {
constexpr int* baz = nullptr;
/* ... */
static_assert(baz, "Ops! Null pointer");
return 0;
}
// http://ideone.com/JNF9sZ
STATIC ASSERTIONS
#if sizeof(unsigned) >= 8
# error "64-bit arch is not supported"
#endif
int main() { return 0; }
#include <type_traits>
static_assert(sizeof(unsigned) >= 8, "64-bit arch is not
supported");
int main() { return 0; }
error: static assertion failed: 64-bit arch is not supported
// http://ideone.com/OmYixS
STATIC ASSERTIONS
#include <type_traits>
template <typename T>
void foobar() {
static_assert(std::is_copy_constructible<T>::value, "Type
T must be copyable");
}
struct Foo { Foo(const Foo&) = delete; };
int main() {
foobar<Foo>();
return 0;
}
error: static assertion failed: Type T must be copyable
// http://ideone.com/DxRSnI
STATIC ASSERTIONS
•Things to remember:
•the compiler evaluates the expression;
•it cannot be used to check assumptions that depends on run-
time values.
PREVENTING EXCEPTION PROPAGATION
// C++98 - no exceptions
void foo() throw()
{
}
•callers might be dependent on the original exception;
•if the exception specification is violated, then
the call stack is unwound to to foo’s caller.
PREVENTING EXCEPTION PROPAGATION
// C++11
void bar() noexcept
{
}
•the stack is only possibly unwound before
program execution is terminated;
•So, optimizers need not keep the runtime stack in an
unwindable state.
•it permits compilers to generate better object code!
PREVENTING EXCEPTION PROPAGATION
// C++11 - cannot throw an exception
void foo() noexcept { }
// as before
void bar() noexcept(true) { }
// permit exceptions to propagate
void qux() noexcept(false) { }
PREVENTING EXCEPTION PROPAGATION
// C++11 - can throw if is not copy constructible
template <typename T>
void foo(T t)
noexcept(std::is_nothrow_copy_constructible<T>::value) { }
// C++11 - evaluate expression
template<typename T>
void foo(T t) {}
template<typename T>
void foobar(T t) noexcept(noexcept(foo(t)))
{
foo(t);
}
PREVENTING EXCEPTION PROPAGATION
•Things to remember:
•‘noexcept’ is simply a shorthand for noexcept(true);
•noexcept(false) specifies that it does permit exceptions to
propagate;
•Use noexcept instead of the exception specifier throw,
which is deprecated in C++11 and later;
• noexcept enables compilers to generate more efficient
code;
•noexcept is particularly valuable for the move operations,
swap, memory deallocation functions, and destructors;
•apply noexcept to a function when you are sure it will never
allow an exception to propagate up the call stack.
OVERRIDE CONTROLS
•Rules for override
•The base class function must be virtual;
•The base and derived function names must be identical
(except in the case of destructors);
•The parameter types of the base and derived functions must
be identical;
•The constness of the base and derived functions must be
identical;
•The return types and exception specifications of the base and
derived functions must be compatible.
•C++11 adds one more:
•The functions’ reference qualifiers must be identical.
OVERRIDE CONTROLS
#include <iostream>
class Qux {
public:
void work() & { std::cout << 1; }
void work() && { std::cout << 2; }
static Qux makeQux() { return Qux{}; }
};
int main() {
Qux::makeQux().work();
auto qux = Qux{};
qux.work();
return 0;
}
fonte: http://ideone.com/EO5YdS
OVERRIDE CONTROLS
class Foo {
public:
virtual void func1() const;
virtual void func2(int i);
virtual void func3() &;
void func4() const;
};
class Bar : public Foo {
public:
virtual void func1();
virtual void func2(unsigned int i);
virtual void func3() &&;
void func4();
};
OVERRIDE CONTROLS
class Foo {
public:
virtual void func1() const;
virtual void func2(int i);
virtual void func3() &;
void func4() const;
};
class Bar : public Foo {
public:
virtual void func1() override;
virtual void func2(unsigned int i) override;
virtual void func3() && override;
void func4() override;
};
fonte: http://ideone.com/VjZiaB
SCOPED AND STRONGLY TYPED ENUMS
The enum classes ("new enums", "strong enums") address three
problems with traditional C++ enumerations:
Conventional enums implicitly convert to int, causing errors when
someone does not want an enumeration to act as an integer;
Conventional enums export their enumerators to the surrounding
scope, causing name clashes.
The underlying type of an enum cannot be specified, causing
confusion, compatibility problems, and makes forward declaration
impossible.
from: http://www.stroustrup.com/C++11FAQ.html#enum
SCOPED AND STRONGLY TYPED ENUMS
// C++98 - unscoped enums.
enum Status { OK, ERROR = 100, INVALID, UNKNOWN = 0xFFFF };
int status = ERROR;
enum Device { UNKNOWN, PRINTER, KEYBOARD };
// error: redeclaration of ‘UNKNOWN’
// C++11 - scoped enums.
enum class Status { OK, ERROR = 100, INVALID, UNKNOWN };
enum class Device { PRINTER, KEYBOARD, UNKNOWN = 0xFFFF };
auto status = UNKNOWN;
// error: ‘UNKNOWN’ was not declared in this scope
auto status = Status::UNKNOWN;
SCOPED AND STRONGLY TYPED ENUMS
// C++11 - Specify the underlying type
#include <iostream>
enum class Signal : char { YES = 'Y', NO = 'N' };
int main() {
char signal = Signal::YES;
std::cout << signal << std::endl;
return 0;
}
error: cannot convert ‘Signal’ to ‘char’ in initialization
auto signal = static_cast<char>(Signal::YES);
SCOPED AND STRONGLY TYPED ENUMS
// C++11 - Specify the underlying type
#include <iostream>
enum class Signal : char;
void foo(Signal signal) {
std::cout << static_cast<char>(signal) << std::endl;
}
enum class Signal : char { YES = 'Y', NO = 'N' };
int main() {
auto signal = Signal::YES;
foo(signal);
return 0;
}
LAMBDA EXPRESSION
template<typename T>
void show(const T& t) {
std::cout << t << std::endl;
}
int main() {
std::vector<std::string> bar{"FOO", "BAR", "QUX"};
std::for_each(bar.begin(), bar.end(),
show<decltype(bar)::value_type>);
return 0;
}
http://ideone.com/U69PEI
LAMBDA EXPRESSION
•About lambda function
•inline anonymous functor;
•specify a simple action to be performed by some function;
•similiar to the idea of a functor or function pointer;
•can use std::function as wrapper.
•Syntax
[ capture-list ] ( params ) -> return-type { body }
LAMBDA EXPRESSION
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<std::string> bar{"FOO", "BAR", "QUX"};
std::for_each(bar.begin(), bar.end(), [ ] (const std::string& ss) {
std::cout << ss << std::endl;
});
return 0;
}
http://ideone.com/TvxZIy
LAMBDA EXPRESSION
using Bar = std::function<double(double, double)>;
int main() {
auto foo = []() { std::cout << "FOO" << std::endl; };
foo();
Bar bar = [] (double a, double b) -> double { return a * b; };
std::cout << bar(5, 2) << std::endl;
std::string ss{"F00B4R"};
[] () { std::cout << ss << std::endl; };
return 0;
}
http://ideone.com/2iU3rc
LAMBDA EXPRESSION
•Capture List
[ ] Capture nothing (or, a scorched earth strategy?)
[&] Capture any referenced variable by reference
[=] Capture any referenced variable by making a copy
[=, &foo] Capture any referenced variable by making a copy,
but capture variable foo by reference
[bar] Capture bar by making a copy; don't copy anything else
[this] Capture the this pointer of the enclosing class
LAMBDA EXPRESSION
// C++14
#include <cassert>
auto foo(auto bar) {
return bar * 2;
}
int main() {
auto baz = [](auto qux) { return qux * 2; };
assert(foo(1));
assert(baz(2));
return 0;
}
http://ideone.com/kjnUme
LAMBDA EXPRESSION
•Things to remember:
•avoid capture any referenced variable by reference (dangling ref);
•Lambdas are more readable, more expressive, and may be more
efficient than using std::bind.
DEFAULTED AND DELETED FUNCTIONS
struct Bar {};
struct Baz { Bar* bar; };
int main() {
Baz baz{new Bar{}};
Baz bazfoo = baz; // bazfoo copy from baz!
return 0;
}
•Prohibiting copying idiom:
•Use boost::noncopyable.
•Declare copy constructor as private
DEFAULTED AND DELETED FUNCTIONS
struct Bar {};
struct Baz {
Bar* bar;
Baz(const Baz&) = delete; // disallow copy constructor
};
int main() {
Baz baz{new Bar{}};
Baz bazfoo = baz; // error: use of deleted function
return 0;
}
DEFAULTED AND DELETED FUNCTIONS
struct BigNumber {
BigNumber(long long) {}
BigNumber(int) = delete;
};
int main()
{
BigNumber(5ll);
BigNumber(42); // deleted function
return 0;
}
DEFAULTED AND DELETED FUNCTIONS
•What are the special member functions of C++11?
•Default constructor C::C();
•Destructor C::~C();
•Copy constructor C::C (const C&);
•Copy assignment C& operator= (const C&);
•Move constructor C::C (C&&);
•Move assignment C& operator= (C&&);
DEFAULTED AND DELETED FUNCTIONS
Special Member Function Implicity defined Default definition
Default constructor if no other constructors does nothing
Destructor if no destructor does nothing
Copy Contructor if no move constructor and
no move assignment
copies all members
Copy Assignment if no move constructor and
no move assignment
copies all members
Move Constructor if no destructor, no copy
constructor and no copy nor
move assignment
moves all members
Move Assignment if no destructor, no copy
constructor and no copy nor
move assignment
moves all members
DEFAULTED AND DELETED FUNCTIONS
#include <utility>
#include <string>
#include <cassert>
struct Foo {
std::string ss;
Foo(const Foo& foo) : ss{foo.ss} {}
Foo(std::string&& s) : ss{std::move(s)} {}
};
int main() {
Foo foo{"FOO"};
Foo foobar(std::move(foo));
assert(foo.ss.empty());
return 0;
}
http://ideone.com/fzZHCB
DEFAULTED AND DELETED FUNCTIONS
#include <utility>
#include <string>
#include <cassert>
struct Foo {
std::string ss;
Foo(const Foo&) = default; // explicitly defaulted
Foo(Foo&&) = default; // explicitly defaulted
Foo(std::string&& s) : ss{std::move(s)} {}
};
int main() {
Foo foo{"FOO"};
Foo foobar(std::move(foo));
assert(foo.ss.empty());
return 0;
}
// http://ideone.com/0Ir7rG
SMART POINTERS
•RAII (Resouce Acquisition Is Initialization)
•Programming technique which binds the life cycle of a resource to
the lifetime of an object with automatic storage duration;
•Scope-Bound Resource Management (SBRM);
•RAII - Summary
•encapsulate each resource into a class, where the constructor
acquires the resource and establishes all class invariants or throws
an exception if that cannot be done, the destructor releases the
resource and never throws exceptions;
•always use the resource via an instance of a RAII-class that either
has automatic storage duration, is a non-static member of a class
whose instance has automatic storage duration.
SMART POINTERS
#include <mutex>
void consummer() {
mutex.lock();
auto worker = work_queue.front();
work_queue.pop();
worker(); // Ops! throw exception
mutex.unlock();
}
SMART POINTERS
#include <mutex>
void consummer() {
std::lock_guard<std::mutex> lock(mutex) // Safe! Using RAII
auto worker = work_queue.front();
work_queue.pop();
worker(); // throw exception. mutex will be unlocked
}
SMART POINTERS
•Look and feel like pointers, but are smarter
•an object that owns another object and manages that other object
through a pointer;
•support pointer operations like dereferencing (operator *) and
indirection (operator ->);
•Automatic cleanup;
•Automatic initialization;
•Exception safety;
SMART POINTERS
template<typename T>
class SmartPointer {
T* t_;
public:
SmartPointer(T* t = 0) : t_(t) {}
~SmartPointer() { delete t_; }
T& operator* () { return *t_; }
T* operator-> () { return t_; }
};
int main() {
SmartPointer<std::string> smartPointer(new std::string("bar"));
std::cout << *smartPointer << std::endl;
std::cout << smartPointer->size() << std::endl;
return 0;
}
SMART POINTERS
•auto_ptr // C++98
•unique_ptr // C++11
•shared_ptr // C++11
•weak_ptr // C++11
SMART POINTERS - AUTO_PTR
// C++98
#include <iostream>
#include <memory>
int main() {
std::auto_ptr<int> foo(new int(1));
std::auto_ptr<int> foobar = foo; // foo will be null
std::cout << *foobar << std::endl;
std::cout << *foo << std::endl; // undefined behavior
return 0;
}
SMART POINTERS - UNIQUE_PTR
// C++11
#include <iostream>
#include <memory>
int main() {
std::unique_ptr<int> foo(new int(1));
std::unique_ptr<int> foobar = foo;
std::cout << *foobar << std::endl;
std::cout << *foo << std::endl;
return 0;
}
error: use of deleted function
std::unique_ptr<int> foobar = foo;
SMART POINTERS - UNIQUE_PTR
class DevicePort {
std::string intf_name;
DevicePort(std::string&& name) : intf_name{std::move(name)} {}
public:
static DevicePort* Create(std::string&& ss) {
return new DevicePort(std::move(ss));
}
void connect() throw (std::runtime_error) {
if (intf_name.find("gigabit") == std::string::npos) {
throw std::runtime_error("Could not connect!");
} else { /* ... */ }
}
};
int main() {
auto port_gb = DevicePort::Create("megabit-ethernet");
try {
port_gb->connect();
delete port_gb;
} catch (const std::runtime_error& err) {
std::cerr << err.what() << std::endl;
}
return 0;
}
SMART POINTERS - UNIQUE_PTR
// #include <...>
template <typename T, typename ...Ts>
std::unique_ptr<T> Factory(Ts&& ...ts) {
return std::unique_ptr<T>(new T(std::forward<Ts>(ts))...);
}
struct DevicePort {
std::string intf_name;
DevicePort(std::string&& name) : intf_name{std::move(name)}
{}
};
int main() {
auto port_gb = Factory<DevicePort>("gigabit-ethernet");
std::cout << port_gb->intf_name << std::endl;
return 0;
SMART POINTERS - UNIQUE_PTR
#include <iostream>
#include <memory>
#include <functional>
struct Bar {
~Bar() { std::cout << 2; }
static void deleter(Bar*) { std::cout << 1; }
};
int main() {
using bar_delete = std::function<void(Bar*)>;
auto bar = std::unique_ptr<Bar, bar_delete>(
new Bar(), Bar::deleter);
return 0;
}
http://ideone.com/8VTUEe
SMART POINTERS - UNIQUE_PTR
•Things to remember:
•strict ownership;
•not copyable;
•moveable;
•deletes using an associated deleter;
•exception safety;
•can be stored in containers;
•return allocated pointer by function.
SMART POINTERS - SHARED_PTR
#include <iostream>
#include <memory>
struct Foo {};
struct Bar { std::shared_ptr<Foo> foo; };
struct Baz { std::shared_ptr<Foo> foo; };
int main() {
auto foo = std::make_shared<Foo>();
Bar bar{ foo };
Baz baz{ foo };
std::cout << foo.use_count() << std::endl; // 3
return 0;
}
http://ideone.com/hAaNjY
SMART POINTERS - SHARED_PTR
#include <memory>
#include <string>
int main() {
auto foobar = new std::string{"F00B4R"};
std::shared_ptr<std::string> foo(foobar);
std::shared_ptr<std::string> bar(foobar);
return 0;
}
http://ideone.com/eqeJGp
SMART POINTERS - SHARED_PTR
https://goo.gl/qDu9SL
SMART POINTERS - SHARED_PTR
•Things to remember:
•shared ownership;
•copyable;
•moveable;
•deletes using an associated deleter;
•exception safety;
•can be stored in containers;
•return allocated pointer by function;
•there is control block;
•avoid create shared_ptr from variables of raw pointers.
SMART POINTERS - WEAK_PTR
struct Port;
struct Aggregation { std::list<std::shared_ptr<Port>> children; };
struct Port { std::shared_ptr<Aggregation> parent; };
int main() {
auto aggregation = std::make_shared<Aggregation>();
auto ports = { std::make_shared<Port>(), std::make_shared<Port>() };
aggregation->children = ports;
for (auto& port : aggregation->children) {
port->parent = aggregation;
}
return 0;
}
// http://ideone.com/YldGSH
SMART POINTERS - WEAK_PTR
struct Port;
struct Aggregation { std::list<std::shared_ptr<Port>> children; };
struct Port { std::weak_ptr<Aggregation> parent; };
int main() {
auto aggregation = std::make_shared<Aggregation>();
auto ports = { std::make_shared<Port>(), std::make_shared<Port>() };
aggregation->children = ports;
for (auto& port : aggregation->children) {
port->parent = aggregation;
}
return 0;
}
// http://ideone.com/fKq2hr
SMART POINTERS - WEAK_PTR
template<typename T>
void show(T& t) { std::cout << (t.lock() ? 0 : 1); }
int main() {
std::weak_ptr<std::string> bar;
{
auto foo = std::make_shared<std::string>("F00B4R");
bar = foo;
show(bar);
}
show(bar);
return 0;
}
// http://ideone.com/uE7kJ9
SMART POINTERS - WEAK_PTR
•Things to remember:
•wraps a weakly linked pointer;
•models temporary ownership;
•break 'circular references' with shared_ptr;
•would be a cache;
•solve the dangling pointer problem.
STANDARD CONTAINER ARRAY
“like a built-in array without the problems” - bjarne
// C++98
static const size_t BUFFER_SIZE = 20;
unsigned buffer [BUFFER_SIZE] = {};
// C++11
#include <array>
constexpr auto BUFFER_SIZE = 20;
std::array<unsigned, BUFFER_SIZE> buffer;
STANDARD CONTAINER ARRAY
// C++11
#include <array>
std::array<int, 5> foo;
auto foobar = foo[3]; // foobar is zero. Default elements are zero
auto* bar = foo;
// error: std::array doesn't implicitly convert to a pointer
auto* bar = foo.data(); // okay!
unsigned baz [] = { 1, 2, 4, 8, 16, 32 };
std::array<unsigned> qux = { 1, 2, 4, 8, 16, 32 };
// error: wrong number of template arguments
STANDARD CONTAINER ARRAY
template <class T> typename T::value_type sum(const T& t)
{
return std::accumulate(t.begin(), t.end(), 0);
}
int main() {
std::vector<unsigned> foo = { 1, 2, 4, 8 };
auto bar = sum(foo); // bar is 15
unsigned qux [] = { 1, 2, 4, 8 };
bar = sum(qux); // error: no matching function for call
std::array<unsigned, 4> baz = { 1, 2, 4, 8 };
bar = sum(baz); // bar is 15
return 0;
}
// http://ideone.com/IAksgE
EXAMPLE - CODE 1
struct foobar {
std::map<std::string, std::string> bar;
std::vector<foobar> qux;
foobar() : bar{{"F00", "B4R"}} {}
~foobar() { bar.clear(); qux.clear(); }
};
int main() {
foobar f;
f.bar["QUX"] = "C0U5&";
return 0;
}
EXAMPLE - CODE 1
#include <map>
#include <vector>
#include <iostream>
struct foobar {
std::map<std::string, std::string> bar = {{"F00", "B4R"}};
std::vector<foobar> qux;
};
int main() {
foobar f;
f.bar["QUX"] = "C0U5&";
return 0;
}
EXAMPLE - CODE 2
struct foobar {
std::vector<unsigned> qux;
void foo() {
int bar; int i;
for (i = 0; i < (int) qux.size(); i++) {
bar = qux.at(i);
std::cout << bar << std::endl;
}
}
};
int main() {
foobar bar;
bar.qux.push_back(42);
bar.foo();
return 0;
}
EXAMPLE - CODE 2
struct foobar {
std::vector<unsigned> qux;
void foo() {
for (const auto& it : qux) {
std::cout << it << std::endl;
}
}
};
int main() {
foobar bar;
bar.qux.emplace_back(42);
bar.foo();
return 0;
}
EXAMPLE - CODE 3
struct Foo {
std::string foobar;
Foo(std::string&& ss) : foobar{std::move(ss)} {}
template <typename String>
static std::unique_ptr<Foo> Factory(String&& ss) {
return std::unique_ptr<Foo>(new
Foo(std::forward<String>(ss)));
}
};
int main() {
auto foo = Foo::Factory("F00B4R");
std::cout << foo->foobar << std::endl;
return 0;
} // http://ideone.com/dhZTRF
CONSTANT EXPRESSIONS
•Use constexpr whenever possible
•Objects: const + value known during compilation.
•Functions: return constexpr result if called with constexpr args.
CONSTANT EXPRESSIONS
•constexpr objects
constexpr auto foo = 42; // Okay!
constexpr auto bar = foo; // Okay!
bar = 11; // error: assignment of read-only variable 'bar'
std::array<unsigned, foo> qux; // Okay!
static_assert(foo == 42, "Foo must be 42!"); // Okay!
int size = 42;
constexpr auto foobar = size; // error: the value of 'size' is not usable in
a constant expression
CONSTANT EXPRESSIONS
•constexpr objects vs const objects
int bar = 42;
const auto foo = bar; // okay: may be initialized at runtime
std::array<unsigned, foo> qux; // error: foo is not a constant expression
•All constexpr objects are const, but not all const objects are constexpr.
•If you need compile-time value, then constexpr is the proper tool.
CONSTANT EXPRESSIONS
•constexpr objects vs const objects
int bar = 42;
const auto foo = bar; // okay: may be initialized at runtime
std::array<unsigned, foo> qux; // error: foo is not a constant expression
•All constexpr objects are const, but not all const objects are constexpr.
•If you need compile-time value, then constexpr is the proper tool.
CONSTANT EXPRESSIONS
•constexpr objects rules:
•may any literal type including:
•float point types
•char literals
•pointer literals
•literal obejcts
•requires no storage declaration
•constexpr parameters not allowed:
void foo(constexpr int bar) // error: could not be constexpr
{
std::array<int, bar> qux;
}
CONSTANT EXPRESSIONS
•constexpr functions:
•execute during compile-time, and return constexpr, only:
•the arguments are constexpr
•the result is used in constexpr context
•execute (perphaps not) during compile-time, when:
•the arguments are constexpr
•the result is not used in constexpr context
•execute at run-time when there is 1 or more non-constexpr arguments.
•no need to overload,
•constexpr function take both constexpr and non-constexpr args.
CONSTANT EXPRESSIONS
constexpr int half_of(double value) noexcept { return value / 2; }
int main() {
constexpr auto foo = half_of(42); // compile-time call
static_assert(foo == 21, "Ops!");
auto bar = half_of(12); // runtime call
assert(bar == 6);
const auto foobar = half_of(12); // compile-time call
static_assert(foobar == 6, "Ops!");
auto qux = 74;
auto couse = half_of(qux); // runtime call
assert(couse == 37);
return 0;
}
CONSTANT EXPRESSIONS
•constexpr functions rules:
•its return type shall be a literal type; and
•its parameter types shall be literal types; and
•its function-body shall be a compound-statement of the form:
{ return expression; }
where expression is a potential constant expression; and
•every implicit conversion used in converting expression to the function
return type shall be one of those allowed in a constant expression.
•constexpr functions and constexpr constructors are implicitly inline
•a constexpr function shall not be virtual
CONSTANT EXPRESSIONS
•constexpr functions:
constexpr int square(int x) { return x * x; } // OK
constexpr long long_max() { return 2147483647; } // OK
constexpr int abs(int x) { return x < 0 ? -x : x; } // OK
constexpr void f(int x) { /* ... */ } // error: return type is void
constexpr int prev(int x) { return --x; } // error: use of decrement
constexpr int g(int x, int n) {
int r = 1; // error: body not just “return expr’’
while (--n > 0) r *= x;
return r;
}
CONSTANT EXPRESSIONS
•Relaxed constexpr restrictions C++14:
•any declaration, except:
•static
•thread_local
•conditional branching statements if and switch;
•any looping statement, including range-based for;
•change the value of an object if the lifetime of that object began within
the constant expression function;
constexpr int pow(int base, int exp) noexcept {
auto result = 1;
for (int i = 0; i < exp; ++i) result *= base;
return result;
}
CONSTANT EXPRESSIONS
•Things to remember:
•constexpr objects are const and are initialized with values known during
compilation.
•constexpr functions can produce compile-time results when called with
arguments whose values are known during compilation.
•constexpr objects and functions may be used in a wider range of
contexts than non-constexpr objects and functions.
•constexpr is part of an object’s or function’s interface.
THE ELEMENTS OF C++11
•How to improve my skills:
•books:
•more effective c++ and modern effective c++; Scott Meyers
•a tour of C++; bjarne stormtrooper
•web
•https://isocpp.org/
•http://scottmeyers.blogspot.com/
•http://herbsutter.com/
•http://erdani.com/
•http://cppcon.org/
•http://cppcast.com/

Contenu connexe

Tendances

Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014fcofdezc
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13julien pauli
 
C++17 std::filesystem - Overview
C++17 std::filesystem - OverviewC++17 std::filesystem - Overview
C++17 std::filesystem - OverviewBartlomiej Filipek
 
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPIPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPGuilherme Blanco
 
Let's talks about string operations in C++17
Let's talks about string operations in C++17Let's talks about string operations in C++17
Let's talks about string operations in C++17Bartlomiej Filipek
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for DummiesElizabeth Smith
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Moriyoshi Koizumi
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumpingMomenMostafa
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)Olve Maudal
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?FITC
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler DevelopmentLogan Chien
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from insidejulien pauli
 

Tendances (20)

Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
Vocabulary Types in C++17
Vocabulary Types in C++17Vocabulary Types in C++17
Vocabulary Types in C++17
 
C++17 std::filesystem - Overview
C++17 std::filesystem - OverviewC++17 std::filesystem - Overview
C++17 std::filesystem - Overview
 
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHPIPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
Let's talks about string operations in C++17
Let's talks about string operations in C++17Let's talks about string operations in C++17
Let's talks about string operations in C++17
 
7 functions
7  functions7  functions
7 functions
 
Modern PHP
Modern PHPModern PHP
Modern PHP
 
Php Extensions for Dummies
Php Extensions for DummiesPhp Extensions for Dummies
Php Extensions for Dummies
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)C++ idioms by example (Nov 2008)
C++ idioms by example (Nov 2008)
 
Sdl Basic
Sdl BasicSdl Basic
Sdl Basic
 
Why TypeScript?
Why TypeScript?Why TypeScript?
Why TypeScript?
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
 
Swift Introduction
Swift IntroductionSwift Introduction
Swift Introduction
 
Quick tour of PHP from inside
Quick tour of PHP from insideQuick tour of PHP from inside
Quick tour of PHP from inside
 

En vedette

C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsStephane Gleizes
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
Web I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolWeb I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolRandy Connolly
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrencyxu liwei
 
Cecti rodrigo lopez - buqueda del clique
Cecti   rodrigo lopez - buqueda del clique Cecti   rodrigo lopez - buqueda del clique
Cecti rodrigo lopez - buqueda del clique Roderik Lowenstein
 
The Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationThe Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationJoel Falcou
 
"Http protocol and other stuff" by Bipin Upadhyay
"Http protocol and other stuff" by Bipin Upadhyay"Http protocol and other stuff" by Bipin Upadhyay
"Http protocol and other stuff" by Bipin UpadhyayBipin Upadhyay
 
HTTP Protocol Basic
HTTP Protocol BasicHTTP Protocol Basic
HTTP Protocol BasicChuong Mai
 
C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/Sławomir Zborowski
 
Networking - TCP/IP stack introduction and IPv6
Networking - TCP/IP stack introduction and IPv6Networking - TCP/IP stack introduction and IPv6
Networking - TCP/IP stack introduction and IPv6Rodolfo Kohn
 
Designing C++ portable SIMD support
Designing C++ portable SIMD supportDesigning C++ portable SIMD support
Designing C++ portable SIMD supportJoel Falcou
 
Database connectivity to sql server asp.net
Database connectivity to sql server asp.netDatabase connectivity to sql server asp.net
Database connectivity to sql server asp.netHemant Sankhla
 

En vedette (20)

C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
 
TCP/IP
TCP/IPTCP/IP
TCP/IP
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Web I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolWeb I - 05 - HTTP Protocol
Web I - 05 - HTTP Protocol
 
Bjarne essencegn13
Bjarne essencegn13Bjarne essencegn13
Bjarne essencegn13
 
C++11
C++11C++11
C++11
 
C++11
C++11C++11
C++11
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
New pop.pptx.pptx.pptx.pptx
New pop.pptx.pptx.pptx.pptxNew pop.pptx.pptx.pptx.pptx
New pop.pptx.pptx.pptx.pptx
 
Cecti rodrigo lopez - buqueda del clique
Cecti   rodrigo lopez - buqueda del clique Cecti   rodrigo lopez - buqueda del clique
Cecti rodrigo lopez - buqueda del clique
 
The Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationThe Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 Migration
 
Sports in the world
Sports in the world Sports in the world
Sports in the world
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
"Http protocol and other stuff" by Bipin Upadhyay
"Http protocol and other stuff" by Bipin Upadhyay"Http protocol and other stuff" by Bipin Upadhyay
"Http protocol and other stuff" by Bipin Upadhyay
 
HTTP Protocol Basic
HTTP Protocol BasicHTTP Protocol Basic
HTTP Protocol Basic
 
C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/
 
Networking - TCP/IP stack introduction and IPv6
Networking - TCP/IP stack introduction and IPv6Networking - TCP/IP stack introduction and IPv6
Networking - TCP/IP stack introduction and IPv6
 
Designing C++ portable SIMD support
Designing C++ portable SIMD supportDesigning C++ portable SIMD support
Designing C++ portable SIMD support
 
Database connectivity to sql server asp.net
Database connectivity to sql server asp.netDatabase connectivity to sql server asp.net
Database connectivity to sql server asp.net
 
Tesis de-albahaca-2016-2017
Tesis de-albahaca-2016-2017Tesis de-albahaca-2016-2017
Tesis de-albahaca-2016-2017
 

Similaire à Elements of C++11

C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeAkira Takahashi
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupSyedHaroonShah4
 
Arduino Section Programming - from Sparkfun
Arduino Section Programming - from SparkfunArduino Section Programming - from Sparkfun
Arduino Section Programming - from SparkfunJhaeZaSangcapGarrido
 
Arduino sectionprogramming slides
Arduino sectionprogramming slidesArduino sectionprogramming slides
Arduino sectionprogramming slidesJorge Joens
 
Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++Mohammad Shaker
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsLourens Naudé
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to DartRamesh Nair
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineMatt Provost
 
NetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf EditionNetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf EditionPaulo Morgado
 

Similaire à Elements of C++11 (20)

C++11
C++11C++11
C++11
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Replace OutputIterator and Extend Range
Replace OutputIterator and Extend RangeReplace OutputIterator and Extend Range
Replace OutputIterator and Extend Range
 
Functions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrupFunctions And Header Files In C++ | Bjarne stroustrup
Functions And Header Files In C++ | Bjarne stroustrup
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Arduino Section Programming - from Sparkfun
Arduino Section Programming - from SparkfunArduino Section Programming - from Sparkfun
Arduino Section Programming - from Sparkfun
 
Arduino sectionprogramming slides
Arduino sectionprogramming slidesArduino sectionprogramming slides
Arduino sectionprogramming slides
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
Intro to C++
Intro to C++Intro to C++
Intro to C++
 
Workshop Swift
Workshop Swift Workshop Swift
Workshop Swift
 
Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++
 
RailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMsRailswayCon 2010 - Dynamic Language VMs
RailswayCon 2010 - Dynamic Language VMs
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to Dart
 
Scope and closures
Scope and closuresScope and closures
Scope and closures
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
NetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf EditionNetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf Edition
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
JavaScript Primer
JavaScript PrimerJavaScript Primer
JavaScript Primer
 
Quick swift tour
Quick swift tourQuick swift tour
Quick swift tour
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 

Plus de Uilian Ries

Gitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CIGitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CIUilian Ries
 
Conan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersConan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersUilian Ries
 
Meetup C++ Floripa - Conan.io
Meetup C++ Floripa - Conan.ioMeetup C++ Floripa - Conan.io
Meetup C++ Floripa - Conan.ioUilian Ries
 
Poco Bibliotecas C++
Poco Bibliotecas C++Poco Bibliotecas C++
Poco Bibliotecas C++Uilian Ries
 
Conan a C/C++ Package Manager
Conan a C/C++ Package ManagerConan a C/C++ Package Manager
Conan a C/C++ Package ManagerUilian Ries
 
Software Development Tools for C/C++
Software Development Tools for C/C++Software Development Tools for C/C++
Software Development Tools for C/C++Uilian Ries
 
Testes Unitários com GTest e Catch
Testes Unitários com GTest e CatchTestes Unitários com GTest e Catch
Testes Unitários com GTest e CatchUilian Ries
 
SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++Uilian Ries
 
Unisinos - Proposta TCC 2015
Unisinos - Proposta TCC 2015Unisinos - Proposta TCC 2015
Unisinos - Proposta TCC 2015Uilian Ries
 

Plus de Uilian Ries (11)

Gitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CIGitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CI
 
Conan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for DevelopersConan.io - The C/C++ package manager for Developers
Conan.io - The C/C++ package manager for Developers
 
Git Workflow
Git WorkflowGit Workflow
Git Workflow
 
BDD em Ação
BDD em AçãoBDD em Ação
BDD em Ação
 
Meetup C++ Floripa - Conan.io
Meetup C++ Floripa - Conan.ioMeetup C++ Floripa - Conan.io
Meetup C++ Floripa - Conan.io
 
Poco Bibliotecas C++
Poco Bibliotecas C++Poco Bibliotecas C++
Poco Bibliotecas C++
 
Conan a C/C++ Package Manager
Conan a C/C++ Package ManagerConan a C/C++ Package Manager
Conan a C/C++ Package Manager
 
Software Development Tools for C/C++
Software Development Tools for C/C++Software Development Tools for C/C++
Software Development Tools for C/C++
 
Testes Unitários com GTest e Catch
Testes Unitários com GTest e CatchTestes Unitários com GTest e Catch
Testes Unitários com GTest e Catch
 
SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++SECCOM 2017 - Conan.io o gerente de pacote para C e C++
SECCOM 2017 - Conan.io o gerente de pacote para C e C++
 
Unisinos - Proposta TCC 2015
Unisinos - Proposta TCC 2015Unisinos - Proposta TCC 2015
Unisinos - Proposta TCC 2015
 

Dernier

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 

Dernier (20)

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Elements of C++11

  • 1. @Uilian Ries 02 / 2016 https://goo.gl/U0fZJa The Elements of C++11
  • 2. AGENDA •Why should I learn about C++11? •C++11 Features •deduction of a type from an initializer •the type of an expression •Initializer-list and uniform initialization •rvalue references and move semantics •static assertions •preventing exception propagation •override controls •scoped and strongly typed enums •lambda expressions •defaulted and deleted functions •smart pointers •standard container array
  • 3. WHY SHOULD I LEARN ABOUT C++11? fonte: https://isocpp.org/files/img/wg21-timeline.png
  • 4. WHY SHOULD I LEARN ABOUT C++11? “C++11 feels like a new language” - Bjarne Stroustrup
  • 5. DEDUCTION OF A TYPE FROM AN INITIALIZER // C++98 std::map<std::string, std::vector<int, int> >::iterator it = foo.begin(); Singleton& singleton = Singleton::instance(); int bar; // C++11 auto it = std::begin(foo); auto& singleton = Singleton::instance(); auto bar; // error: declaration has no initializer auto bar = 0;
  • 6. DEDUCTION OF A TYPE FROM AN INITIALIZER // C++98 std::vector<std::string>::const_iterator it; for (it = strings.begin(); it != strings.end(); ++it) { /*...*/ } // C++11 for (auto it : strings) { /*...*/ }
  • 7. DEDUCTION OF A TYPE FROM AN INITIALIZER // C++11 auto sum(int a, int b) -> int { return a + b; } template<typename Builder> void buildAndProcess(Builder& builder) { auto baz = builder.Build(); baz.work(); }
  • 8. DEDUCTION OF A TYPE FROM AN INITIALIZER // C++11 template<typename Builder> auto buildAndProcess(Builder& builder) -> ??? { auto baz = builder.Build(); baz.work(); return baz; }
  • 9. THE TYPE OF AN EXPRESSION •decltype •Auto lets you declare a variable with a particular type • decltype lets you extract the type from a variable (or any other expression)
  • 10. THE TYPE OF AN EXPRESSION // C++11 int bar = 42; decltype(bar) qux = bar; auto qux = bar; // same int qux(); decltype(qux()); // int
  • 11. DEDUCTION OF A TYPE FROM AN INITIALIZER // C++11 template<typename Builder> auto buildAndProcess(Builder& builder) -> decltype( builder.Build() ) { auto baz = builder.Build(); baz.work(); return baz; } // C++14 template<typename Builder> auto buildAndProcess(Builder& builder) { auto baz = builder.Build(); baz.work(); return baz; }
  • 12. DEDUCTION OF A TYPE FROM AN INITIALIZER #include <iostream> struct Foo { Foo () { std::cout << 1; } ~Foo() { std::cout << 3; } void print() { std::cout << 2; } }; int main() { Foo foo[3]; for (auto it : foo) { it.print(); } return 0; } fonte: http://ideone.com/ux5CeG for (auto& it : foo) { it.print(); }
  • 13. DEDUCTION OF A TYPE FROM AN INITIALIZER // C++11 int& bar(); auto foo = bar(); // int or int& ? /* auto defaults to being by-value for references */ auto& foo = bar(); // int& int* foo = new int(5); auto bar = foo; // bar is int* auto* qux = foo; // as before
  • 14. DEDUCTION OF A TYPE FROM AN INITIALIZER std::string foo("foo"); unsigned size = foo.size(); // type shortcut std::string::size_type size = foo.size(); // official return type auto size = foo.size(); // correct deduction
  • 15. DEDUCTION OF A TYPE FROM AN INITIALIZER // C++11 #include <unordered_map> std::unordered_map<std::string, std::string> bar; // bar receives some elements ... for (const std::pair<std::string, std::string>& it : bar) { /* ... */ } for (const auto& it : bar) { /* ... */ }
  • 16. DEDUCTION OF A TYPE FROM AN INITIALIZER •Things to remember: •auto must be initialized; •the compiler can evaluates better than you; •auto can simplify your expression; •a function can return auto type; •a function parameter can be auto type (C++14); •auto defaults to being by-value for references.
  • 17. INITIALIZER-LIST AND UNIFORM INITIALIZATION // C++98 int foo[] = {16,2,77,29}; std::vector<int> bar(foo, foo + sizeof(foo) / sizeof(foo[0])); // C++98 std::map<int, bool> qux; qux[100] = true; qux[110] = false; // C++11 std::vector<int> bar = {16,2,77,29}; // C++11 std::map<int, bool> qux = {{100, true}, {110, false}};
  • 18. INITIALIZER-LIST AND UNIFORM INITIALIZATION #include <iostream> #include <initializer_list> struct Foo { Foo(int a, int b) { std::cout << a << " " << b << std::endl; } Foo (std::initializer_list<int> list) { for (auto it : list) { std::cout << it << std::endl; } } }; int main() { Foo foo{1, 2}; Foo foobar(1, 2); return 0; }
  • 19. INITIALIZER-LIST AND UNIFORM INITIALIZATION // C++98 int foo = 5.2; // narrowing convertion int bar(3.14); // narrowing convertion // C++11 int foo{5.2}; // error: narrowing convertion int qux[3]{1, 2, 3}; std::vector<char> baz{'a', 'b', 'c'}; // C++98 Baz::Baz(int bar, int corge) : bar_(bar), corge_(corge) {} insert_vlan( vlan(100, TAGGED) ); // C++11 Baz::Baz(int bar, int corge) : bar_{bar}, corge_{corge} {} insert_vlan( {100, TAGGED} );
  • 20. INITIALIZER-LIST AND UNIFORM INITIALIZATION •initializer-list •array of objects of type const T; •must be homogeneous; •immutable sequence. std::initializer_list<int> qux{1, 2, 3, 4, 5}; // Okay std::initializer_list<int> qux{1, 2, 3.0, 4, 5}; // NOK! error: narrowing conversion of '3.0e+0' from 'double' to 'int' inside { } [-Wnarrowing] std::initializer_list<int> qux{1, 2, 3, 4, 5}; *qux.begin() = 42; error: assignment of read-only location
  • 21. INITIALIZER-LIST AND UNIFORM INITIALIZATION #include <iostream> #include <type_traits> int main() { auto foo{5}; auto bar = {3, 5}; auto qux{4, 2}; std::cout << std::is_integral<decltype(foo)>::value; std::cout << std::is_integral<decltype(bat)>::value; std::cout << std::is_integral<decltype(qux)>::value; return 0; } fonte: http://ideone.com/QMhxvv auto foo = 5; error: direct-list-initialization of 'auto' requires exactly one element [- fpermissive] auto qux{4, 2};
  • 22. RVALUE REFERENCES AND MOVE SEMANTICS •In C++11, there are rvalues and lvalues: •lvalues is an expression that could be the left hand side of an assignment; •rvalues is an expression that could be the right hand side of an assignment; int foo = 42; // foo is a lvalue; 42 is an rvalue
  • 23. RVALUE REFERENCES AND MOVE SEMANTICS •References: •lvalue reference is formed by placing an & after some type; •rvalue reference is formed by placing an && after some type; int bar = 42; // bar is a lvalue int& bar_ref = bar; // bar_ref is a lvalue reference int& baz = 42; // error: can't bind rvalue const int& baz = 42; // okay! baz is const reference int&& qux = 42; // qux is an rvalue reference qux = 54; // okay! can replace int&& corge = bar; // error: can't bind lvalue
  • 24. RVALUE REFERENCES AND MOVE SEMANTICS •Why would we want to do this?: •move semantics; •perfect forward;
  • 25. RVALUE REFERENCES AND MOVE SEMANTICS •Move semantics •replaces expansive copy with less expansive move; •enables the creation of move-only type as unique_ptr; •Consider: template <class T> swap(T& a, T& b) { T tmp(a); // now we have two copies of a a = b; // now we have two copies of b b = tmp; // now we have two copies of tmp (aka a) }
  • 26. RVALUE REFERENCES AND MOVE SEMANTICS #include <utility> template <class T> swap(T& a, T& b) { T tmp(std::move(a)); // move a contents to tmp; a is empty a = std::move(b); // move b contents to a; b is empty b = std::move(tmp); // move tmp contents to b; tmp is empty } •move gives its target the value of its argument, but is not obliged to preserve the value of its source; •if move is not possible, then the value will be copied.
  • 27. RVALUE REFERENCES AND MOVE SEMANTICS class Foo { // Temporary Object std::string bar; public: Foo(const std::string& ss) : bar(ss) { assert(!bar.empty()); // ss was copied! } }; int main() { std::string baz("F00B4R"); Foo foo(baz); assert(!baz.empty()); // baz is no more necessary! return 0; } http://ideone.com/fzZHCB
  • 28. RVALUE REFERENCES AND MOVE SEMANTICS #include <utility> // Temporary Object class Foo { std::string bar; public: Foo(std::string&& ss) : bar(std::move(ss)) { assert(!bar.empty()); // ss was moved! } }; int main() { std::string baz("F00B4R"); Foo foo(std::move(baz)); assert(baz.empty()); // baz was moved! return 0; } http://ideone.com/fzZHCB
  • 29. RVALUE REFERENCES AND MOVE SEMANTICS •About move and forward functions •move doesn’t move; •forward doesn’t forward; •neither does anything at runtime; •generate nothing.
  • 30. RVALUE REFERENCES AND MOVE SEMANTICS •About move and forward functions •move and forward are functions that performs casts; •move is a unconditional cast to an rvalue; •forward performs this cast only if a particular condition is fulfilled. template<typename T> typename remove_reference<T>::type&& move(T&& param) { using ReturnType = typename remove_reference<T>::type&&; return static_cast<ReturnType>(param); } •move returns a && type (rvalue reference); •remove_reference ensures that && is applied only to non-reference.
  • 31. RVALUE REFERENCES AND MOVE SEMANTICS •Perfect Forward •write function templates that take arbitrary arguments; •forward exactly the same arguments as were passed. •Consider: template <class T> std::shared_ptr<T> factory() { return std::make_shared<T>(); }
  • 32. RVALUE REFERENCES AND MOVE SEMANTICS template <class T, class Arg> std::shared_ptr<T> factory(Arg& arg) { return std::make_shared<T>(arg); } int main() { std::string ss("FOO"); auto foo = factory<std::string>(ss); auto bar = factory<std::string>(std::move(ss)); // error! return 0; }
  • 33. RVALUE REFERENCES AND MOVE SEMANTICS template <class T, class Args> std::shared_ptr<T> factory(Args&& args) { return std::make_shared<T>(std::forward<Args>(args)); } •forward preserves the lvalue/rvalue-ness of the argument that was passed to factory; •Args&& can be anything.
  • 34. RVALUE REFERENCES AND MOVE SEMANTICS •Things to remember •move can be cheaper than copy; •std::move doesn’t move; it’s an unconditional cast to an rvalue; •std::forward doesn’t forward; it’s casts its argument to an rvalue only if that argument is bound to an rvalue;
  • 35. RVALUE REFERENCES AND MOVE SEMANTICS •Universal References •sometimes && is not a rvalue reference. class Foo{}; // ... Foo&& foo = Foo(); // && means rvalue reference auto&& bar = foo; // && doesn't mean rvalue reference template<typename T> void process(std::vector<T>&& args); // && means rvalue reference template<typename T> void process(T&& args); // && doesn't mean rvalue reference
  • 36. RVALUE REFERENCES AND MOVE SEMANTICS •Universal References •can binds rvalue reference; •can binds lvalue reference; •can binds const or non-consts objects. class Foo{}; // ... Foo&& foo = Foo(); // we can obtain the address of foo auto&& bar = foo; // so, bar is a reference to a lvalue Foo& bar = foo; // same
  • 37. RVALUE REFERENCES AND MOVE SEMANTICS •Universal References template<typename T> void process(T&& args) {} process(10); // 10 is a rvalue; T&& is a rvalue reference Foo foo; process(foo); // foo is a lvalue; T&& is a lvalue reference, as Foo& template <typename T> void foobar(const T&& param) {} // const qualifier disables uref
  • 38. RVALUE REFERENCES AND MOVE SEMANTICS •Universal References •use move on rvalue references and forward on universal references; class Foo { std::string bar; public: template <typename T> void set_bar(T&& t) { bar = std::move(t); } }; int main() { std::string baz{"F00B4R"}; Foo foo; foo.set_bar(baz); assert(!baz.empty()); // Ops! return 0; } http://ideone.com/MXdPXx
  • 39. RVALUE REFERENCES AND MOVE SEMANTICS •Universal References •use move on rvalue references and forward on universal references; class Foo { std::string bar; public: void set_bar(std::string&& ss) { bar = std::move(ss); } }; int main() { std::string baz{"F00B4R"}; Foo foo; foo.set_bar(baz); // error: no mathing function assert(!baz.empty()); return 0; } http://ideone.com/WFlcon
  • 40. RVALUE REFERENCES AND MOVE SEMANTICS •Things to remember: Universal References •auto&& and T&& for a deduces type T, are universal reference; •universal references are lvalue references when initialized with lvalues; •universal references are rvalue references when initialized with rvalues; •use move on rvalue references; •use forward on universal references.
  • 41. RVALUE REFERENCES AND MOVE SEMANTICS •Consider pass by value when cheaper: Overloading class Foo { std::vector<std::string> foobar; public: void insert(const std::string& ss) { foobar.push_back(ss); // Copy! } void insert(std::string&& ss) { foobar.push_back(std::move(ss)); } }; int main() { Foo foo; std::string bar("B4R"); foo.insert(bar); // insert by lvalue foo.insert("F00"); // insert by rvalue return 0; } // http://ideone.com/Qb3ASJ
  • 42. RVALUE REFERENCES AND MOVE SEMANTICS •Consider pass by value when cheaper: Universal Reference class Foo { std::vector<std::string> foobar; public: template <typename T> void insert(T&& ss) { foobar.push_back(std::forward<T>(ss)); } }; int main() { Foo foo; std::string bar("B4R"); foo.insert(bar); // insert by lvalue foo.insert("F00"); // insert by rvalue return 0; } http://ideone.com/EPmSho
  • 43. RVALUE REFERENCES AND MOVE SEMANTICS •Consider pass by value when cheaper: Passing by value class Foo { std::vector<std::string> foobar; public: void insert(std::string ss) { foobar.push_back(std::move(ss)); } }; int main() { Foo foo; std::string bar("B4R"); foo.insert(bar); // insert by lvalue foo.insert("F00"); // insert by rvalue return 0; } http://ideone.com/c1Hsv7
  • 44. RVALUE REFERENCES AND MOVE SEMANTICS •Overloading •one copy for lvalues; •one move for rvalues. •Universal Reference •one copy for lvalues; •one move for rvalues. •Passing by value •one copy + one move for lvalues; •two moves for rvalues; + parameters that are always copied; + nearly efficient as pass by reference; + easier to implement; + can generate less object code.
  • 45. STATIC ASSERTIONS ● static_assert performs compile time assertion checking. static_assert ( bool_constexpr , message ); #include <type_traits> int main() { constexpr int* baz = nullptr; /* ... */ static_assert(baz, "Ops! Null pointer"); return 0; } // http://ideone.com/JNF9sZ
  • 46. STATIC ASSERTIONS #if sizeof(unsigned) >= 8 # error "64-bit arch is not supported" #endif int main() { return 0; } #include <type_traits> static_assert(sizeof(unsigned) >= 8, "64-bit arch is not supported"); int main() { return 0; } error: static assertion failed: 64-bit arch is not supported // http://ideone.com/OmYixS
  • 47. STATIC ASSERTIONS #include <type_traits> template <typename T> void foobar() { static_assert(std::is_copy_constructible<T>::value, "Type T must be copyable"); } struct Foo { Foo(const Foo&) = delete; }; int main() { foobar<Foo>(); return 0; } error: static assertion failed: Type T must be copyable // http://ideone.com/DxRSnI
  • 48. STATIC ASSERTIONS •Things to remember: •the compiler evaluates the expression; •it cannot be used to check assumptions that depends on run- time values.
  • 49. PREVENTING EXCEPTION PROPAGATION // C++98 - no exceptions void foo() throw() { } •callers might be dependent on the original exception; •if the exception specification is violated, then the call stack is unwound to to foo’s caller.
  • 50. PREVENTING EXCEPTION PROPAGATION // C++11 void bar() noexcept { } •the stack is only possibly unwound before program execution is terminated; •So, optimizers need not keep the runtime stack in an unwindable state. •it permits compilers to generate better object code!
  • 51. PREVENTING EXCEPTION PROPAGATION // C++11 - cannot throw an exception void foo() noexcept { } // as before void bar() noexcept(true) { } // permit exceptions to propagate void qux() noexcept(false) { }
  • 52. PREVENTING EXCEPTION PROPAGATION // C++11 - can throw if is not copy constructible template <typename T> void foo(T t) noexcept(std::is_nothrow_copy_constructible<T>::value) { } // C++11 - evaluate expression template<typename T> void foo(T t) {} template<typename T> void foobar(T t) noexcept(noexcept(foo(t))) { foo(t); }
  • 53. PREVENTING EXCEPTION PROPAGATION •Things to remember: •‘noexcept’ is simply a shorthand for noexcept(true); •noexcept(false) specifies that it does permit exceptions to propagate; •Use noexcept instead of the exception specifier throw, which is deprecated in C++11 and later; • noexcept enables compilers to generate more efficient code; •noexcept is particularly valuable for the move operations, swap, memory deallocation functions, and destructors; •apply noexcept to a function when you are sure it will never allow an exception to propagate up the call stack.
  • 54. OVERRIDE CONTROLS •Rules for override •The base class function must be virtual; •The base and derived function names must be identical (except in the case of destructors); •The parameter types of the base and derived functions must be identical; •The constness of the base and derived functions must be identical; •The return types and exception specifications of the base and derived functions must be compatible. •C++11 adds one more: •The functions’ reference qualifiers must be identical.
  • 55. OVERRIDE CONTROLS #include <iostream> class Qux { public: void work() & { std::cout << 1; } void work() && { std::cout << 2; } static Qux makeQux() { return Qux{}; } }; int main() { Qux::makeQux().work(); auto qux = Qux{}; qux.work(); return 0; } fonte: http://ideone.com/EO5YdS
  • 56. OVERRIDE CONTROLS class Foo { public: virtual void func1() const; virtual void func2(int i); virtual void func3() &; void func4() const; }; class Bar : public Foo { public: virtual void func1(); virtual void func2(unsigned int i); virtual void func3() &&; void func4(); };
  • 57. OVERRIDE CONTROLS class Foo { public: virtual void func1() const; virtual void func2(int i); virtual void func3() &; void func4() const; }; class Bar : public Foo { public: virtual void func1() override; virtual void func2(unsigned int i) override; virtual void func3() && override; void func4() override; }; fonte: http://ideone.com/VjZiaB
  • 58. SCOPED AND STRONGLY TYPED ENUMS The enum classes ("new enums", "strong enums") address three problems with traditional C++ enumerations: Conventional enums implicitly convert to int, causing errors when someone does not want an enumeration to act as an integer; Conventional enums export their enumerators to the surrounding scope, causing name clashes. The underlying type of an enum cannot be specified, causing confusion, compatibility problems, and makes forward declaration impossible. from: http://www.stroustrup.com/C++11FAQ.html#enum
  • 59. SCOPED AND STRONGLY TYPED ENUMS // C++98 - unscoped enums. enum Status { OK, ERROR = 100, INVALID, UNKNOWN = 0xFFFF }; int status = ERROR; enum Device { UNKNOWN, PRINTER, KEYBOARD }; // error: redeclaration of ‘UNKNOWN’ // C++11 - scoped enums. enum class Status { OK, ERROR = 100, INVALID, UNKNOWN }; enum class Device { PRINTER, KEYBOARD, UNKNOWN = 0xFFFF }; auto status = UNKNOWN; // error: ‘UNKNOWN’ was not declared in this scope auto status = Status::UNKNOWN;
  • 60. SCOPED AND STRONGLY TYPED ENUMS // C++11 - Specify the underlying type #include <iostream> enum class Signal : char { YES = 'Y', NO = 'N' }; int main() { char signal = Signal::YES; std::cout << signal << std::endl; return 0; } error: cannot convert ‘Signal’ to ‘char’ in initialization auto signal = static_cast<char>(Signal::YES);
  • 61. SCOPED AND STRONGLY TYPED ENUMS // C++11 - Specify the underlying type #include <iostream> enum class Signal : char; void foo(Signal signal) { std::cout << static_cast<char>(signal) << std::endl; } enum class Signal : char { YES = 'Y', NO = 'N' }; int main() { auto signal = Signal::YES; foo(signal); return 0; }
  • 62. LAMBDA EXPRESSION template<typename T> void show(const T& t) { std::cout << t << std::endl; } int main() { std::vector<std::string> bar{"FOO", "BAR", "QUX"}; std::for_each(bar.begin(), bar.end(), show<decltype(bar)::value_type>); return 0; } http://ideone.com/U69PEI
  • 63. LAMBDA EXPRESSION •About lambda function •inline anonymous functor; •specify a simple action to be performed by some function; •similiar to the idea of a functor or function pointer; •can use std::function as wrapper. •Syntax [ capture-list ] ( params ) -> return-type { body }
  • 64. LAMBDA EXPRESSION #include <string> #include <vector> #include <algorithm> #include <iostream> int main() { std::vector<std::string> bar{"FOO", "BAR", "QUX"}; std::for_each(bar.begin(), bar.end(), [ ] (const std::string& ss) { std::cout << ss << std::endl; }); return 0; } http://ideone.com/TvxZIy
  • 65. LAMBDA EXPRESSION using Bar = std::function<double(double, double)>; int main() { auto foo = []() { std::cout << "FOO" << std::endl; }; foo(); Bar bar = [] (double a, double b) -> double { return a * b; }; std::cout << bar(5, 2) << std::endl; std::string ss{"F00B4R"}; [] () { std::cout << ss << std::endl; }; return 0; } http://ideone.com/2iU3rc
  • 66. LAMBDA EXPRESSION •Capture List [ ] Capture nothing (or, a scorched earth strategy?) [&] Capture any referenced variable by reference [=] Capture any referenced variable by making a copy [=, &foo] Capture any referenced variable by making a copy, but capture variable foo by reference [bar] Capture bar by making a copy; don't copy anything else [this] Capture the this pointer of the enclosing class
  • 67. LAMBDA EXPRESSION // C++14 #include <cassert> auto foo(auto bar) { return bar * 2; } int main() { auto baz = [](auto qux) { return qux * 2; }; assert(foo(1)); assert(baz(2)); return 0; } http://ideone.com/kjnUme
  • 68. LAMBDA EXPRESSION •Things to remember: •avoid capture any referenced variable by reference (dangling ref); •Lambdas are more readable, more expressive, and may be more efficient than using std::bind.
  • 69. DEFAULTED AND DELETED FUNCTIONS struct Bar {}; struct Baz { Bar* bar; }; int main() { Baz baz{new Bar{}}; Baz bazfoo = baz; // bazfoo copy from baz! return 0; } •Prohibiting copying idiom: •Use boost::noncopyable. •Declare copy constructor as private
  • 70. DEFAULTED AND DELETED FUNCTIONS struct Bar {}; struct Baz { Bar* bar; Baz(const Baz&) = delete; // disallow copy constructor }; int main() { Baz baz{new Bar{}}; Baz bazfoo = baz; // error: use of deleted function return 0; }
  • 71. DEFAULTED AND DELETED FUNCTIONS struct BigNumber { BigNumber(long long) {} BigNumber(int) = delete; }; int main() { BigNumber(5ll); BigNumber(42); // deleted function return 0; }
  • 72. DEFAULTED AND DELETED FUNCTIONS •What are the special member functions of C++11? •Default constructor C::C(); •Destructor C::~C(); •Copy constructor C::C (const C&); •Copy assignment C& operator= (const C&); •Move constructor C::C (C&&); •Move assignment C& operator= (C&&);
  • 73. DEFAULTED AND DELETED FUNCTIONS Special Member Function Implicity defined Default definition Default constructor if no other constructors does nothing Destructor if no destructor does nothing Copy Contructor if no move constructor and no move assignment copies all members Copy Assignment if no move constructor and no move assignment copies all members Move Constructor if no destructor, no copy constructor and no copy nor move assignment moves all members Move Assignment if no destructor, no copy constructor and no copy nor move assignment moves all members
  • 74. DEFAULTED AND DELETED FUNCTIONS #include <utility> #include <string> #include <cassert> struct Foo { std::string ss; Foo(const Foo& foo) : ss{foo.ss} {} Foo(std::string&& s) : ss{std::move(s)} {} }; int main() { Foo foo{"FOO"}; Foo foobar(std::move(foo)); assert(foo.ss.empty()); return 0; } http://ideone.com/fzZHCB
  • 75. DEFAULTED AND DELETED FUNCTIONS #include <utility> #include <string> #include <cassert> struct Foo { std::string ss; Foo(const Foo&) = default; // explicitly defaulted Foo(Foo&&) = default; // explicitly defaulted Foo(std::string&& s) : ss{std::move(s)} {} }; int main() { Foo foo{"FOO"}; Foo foobar(std::move(foo)); assert(foo.ss.empty()); return 0; } // http://ideone.com/0Ir7rG
  • 76. SMART POINTERS •RAII (Resouce Acquisition Is Initialization) •Programming technique which binds the life cycle of a resource to the lifetime of an object with automatic storage duration; •Scope-Bound Resource Management (SBRM); •RAII - Summary •encapsulate each resource into a class, where the constructor acquires the resource and establishes all class invariants or throws an exception if that cannot be done, the destructor releases the resource and never throws exceptions; •always use the resource via an instance of a RAII-class that either has automatic storage duration, is a non-static member of a class whose instance has automatic storage duration.
  • 77. SMART POINTERS #include <mutex> void consummer() { mutex.lock(); auto worker = work_queue.front(); work_queue.pop(); worker(); // Ops! throw exception mutex.unlock(); }
  • 78. SMART POINTERS #include <mutex> void consummer() { std::lock_guard<std::mutex> lock(mutex) // Safe! Using RAII auto worker = work_queue.front(); work_queue.pop(); worker(); // throw exception. mutex will be unlocked }
  • 79. SMART POINTERS •Look and feel like pointers, but are smarter •an object that owns another object and manages that other object through a pointer; •support pointer operations like dereferencing (operator *) and indirection (operator ->); •Automatic cleanup; •Automatic initialization; •Exception safety;
  • 80. SMART POINTERS template<typename T> class SmartPointer { T* t_; public: SmartPointer(T* t = 0) : t_(t) {} ~SmartPointer() { delete t_; } T& operator* () { return *t_; } T* operator-> () { return t_; } }; int main() { SmartPointer<std::string> smartPointer(new std::string("bar")); std::cout << *smartPointer << std::endl; std::cout << smartPointer->size() << std::endl; return 0; }
  • 81. SMART POINTERS •auto_ptr // C++98 •unique_ptr // C++11 •shared_ptr // C++11 •weak_ptr // C++11
  • 82. SMART POINTERS - AUTO_PTR // C++98 #include <iostream> #include <memory> int main() { std::auto_ptr<int> foo(new int(1)); std::auto_ptr<int> foobar = foo; // foo will be null std::cout << *foobar << std::endl; std::cout << *foo << std::endl; // undefined behavior return 0; }
  • 83. SMART POINTERS - UNIQUE_PTR // C++11 #include <iostream> #include <memory> int main() { std::unique_ptr<int> foo(new int(1)); std::unique_ptr<int> foobar = foo; std::cout << *foobar << std::endl; std::cout << *foo << std::endl; return 0; } error: use of deleted function std::unique_ptr<int> foobar = foo;
  • 84. SMART POINTERS - UNIQUE_PTR class DevicePort { std::string intf_name; DevicePort(std::string&& name) : intf_name{std::move(name)} {} public: static DevicePort* Create(std::string&& ss) { return new DevicePort(std::move(ss)); } void connect() throw (std::runtime_error) { if (intf_name.find("gigabit") == std::string::npos) { throw std::runtime_error("Could not connect!"); } else { /* ... */ } } }; int main() { auto port_gb = DevicePort::Create("megabit-ethernet"); try { port_gb->connect(); delete port_gb; } catch (const std::runtime_error& err) { std::cerr << err.what() << std::endl; } return 0; }
  • 85. SMART POINTERS - UNIQUE_PTR // #include <...> template <typename T, typename ...Ts> std::unique_ptr<T> Factory(Ts&& ...ts) { return std::unique_ptr<T>(new T(std::forward<Ts>(ts))...); } struct DevicePort { std::string intf_name; DevicePort(std::string&& name) : intf_name{std::move(name)} {} }; int main() { auto port_gb = Factory<DevicePort>("gigabit-ethernet"); std::cout << port_gb->intf_name << std::endl; return 0;
  • 86. SMART POINTERS - UNIQUE_PTR #include <iostream> #include <memory> #include <functional> struct Bar { ~Bar() { std::cout << 2; } static void deleter(Bar*) { std::cout << 1; } }; int main() { using bar_delete = std::function<void(Bar*)>; auto bar = std::unique_ptr<Bar, bar_delete>( new Bar(), Bar::deleter); return 0; } http://ideone.com/8VTUEe
  • 87. SMART POINTERS - UNIQUE_PTR •Things to remember: •strict ownership; •not copyable; •moveable; •deletes using an associated deleter; •exception safety; •can be stored in containers; •return allocated pointer by function.
  • 88. SMART POINTERS - SHARED_PTR #include <iostream> #include <memory> struct Foo {}; struct Bar { std::shared_ptr<Foo> foo; }; struct Baz { std::shared_ptr<Foo> foo; }; int main() { auto foo = std::make_shared<Foo>(); Bar bar{ foo }; Baz baz{ foo }; std::cout << foo.use_count() << std::endl; // 3 return 0; } http://ideone.com/hAaNjY
  • 89. SMART POINTERS - SHARED_PTR #include <memory> #include <string> int main() { auto foobar = new std::string{"F00B4R"}; std::shared_ptr<std::string> foo(foobar); std::shared_ptr<std::string> bar(foobar); return 0; } http://ideone.com/eqeJGp
  • 90. SMART POINTERS - SHARED_PTR https://goo.gl/qDu9SL
  • 91. SMART POINTERS - SHARED_PTR •Things to remember: •shared ownership; •copyable; •moveable; •deletes using an associated deleter; •exception safety; •can be stored in containers; •return allocated pointer by function; •there is control block; •avoid create shared_ptr from variables of raw pointers.
  • 92. SMART POINTERS - WEAK_PTR struct Port; struct Aggregation { std::list<std::shared_ptr<Port>> children; }; struct Port { std::shared_ptr<Aggregation> parent; }; int main() { auto aggregation = std::make_shared<Aggregation>(); auto ports = { std::make_shared<Port>(), std::make_shared<Port>() }; aggregation->children = ports; for (auto& port : aggregation->children) { port->parent = aggregation; } return 0; } // http://ideone.com/YldGSH
  • 93. SMART POINTERS - WEAK_PTR struct Port; struct Aggregation { std::list<std::shared_ptr<Port>> children; }; struct Port { std::weak_ptr<Aggregation> parent; }; int main() { auto aggregation = std::make_shared<Aggregation>(); auto ports = { std::make_shared<Port>(), std::make_shared<Port>() }; aggregation->children = ports; for (auto& port : aggregation->children) { port->parent = aggregation; } return 0; } // http://ideone.com/fKq2hr
  • 94. SMART POINTERS - WEAK_PTR template<typename T> void show(T& t) { std::cout << (t.lock() ? 0 : 1); } int main() { std::weak_ptr<std::string> bar; { auto foo = std::make_shared<std::string>("F00B4R"); bar = foo; show(bar); } show(bar); return 0; } // http://ideone.com/uE7kJ9
  • 95. SMART POINTERS - WEAK_PTR •Things to remember: •wraps a weakly linked pointer; •models temporary ownership; •break 'circular references' with shared_ptr; •would be a cache; •solve the dangling pointer problem.
  • 96. STANDARD CONTAINER ARRAY “like a built-in array without the problems” - bjarne // C++98 static const size_t BUFFER_SIZE = 20; unsigned buffer [BUFFER_SIZE] = {}; // C++11 #include <array> constexpr auto BUFFER_SIZE = 20; std::array<unsigned, BUFFER_SIZE> buffer;
  • 97. STANDARD CONTAINER ARRAY // C++11 #include <array> std::array<int, 5> foo; auto foobar = foo[3]; // foobar is zero. Default elements are zero auto* bar = foo; // error: std::array doesn't implicitly convert to a pointer auto* bar = foo.data(); // okay! unsigned baz [] = { 1, 2, 4, 8, 16, 32 }; std::array<unsigned> qux = { 1, 2, 4, 8, 16, 32 }; // error: wrong number of template arguments
  • 98. STANDARD CONTAINER ARRAY template <class T> typename T::value_type sum(const T& t) { return std::accumulate(t.begin(), t.end(), 0); } int main() { std::vector<unsigned> foo = { 1, 2, 4, 8 }; auto bar = sum(foo); // bar is 15 unsigned qux [] = { 1, 2, 4, 8 }; bar = sum(qux); // error: no matching function for call std::array<unsigned, 4> baz = { 1, 2, 4, 8 }; bar = sum(baz); // bar is 15 return 0; } // http://ideone.com/IAksgE
  • 99. EXAMPLE - CODE 1 struct foobar { std::map<std::string, std::string> bar; std::vector<foobar> qux; foobar() : bar{{"F00", "B4R"}} {} ~foobar() { bar.clear(); qux.clear(); } }; int main() { foobar f; f.bar["QUX"] = "C0U5&"; return 0; }
  • 100. EXAMPLE - CODE 1 #include <map> #include <vector> #include <iostream> struct foobar { std::map<std::string, std::string> bar = {{"F00", "B4R"}}; std::vector<foobar> qux; }; int main() { foobar f; f.bar["QUX"] = "C0U5&"; return 0; }
  • 101. EXAMPLE - CODE 2 struct foobar { std::vector<unsigned> qux; void foo() { int bar; int i; for (i = 0; i < (int) qux.size(); i++) { bar = qux.at(i); std::cout << bar << std::endl; } } }; int main() { foobar bar; bar.qux.push_back(42); bar.foo(); return 0; }
  • 102. EXAMPLE - CODE 2 struct foobar { std::vector<unsigned> qux; void foo() { for (const auto& it : qux) { std::cout << it << std::endl; } } }; int main() { foobar bar; bar.qux.emplace_back(42); bar.foo(); return 0; }
  • 103. EXAMPLE - CODE 3 struct Foo { std::string foobar; Foo(std::string&& ss) : foobar{std::move(ss)} {} template <typename String> static std::unique_ptr<Foo> Factory(String&& ss) { return std::unique_ptr<Foo>(new Foo(std::forward<String>(ss))); } }; int main() { auto foo = Foo::Factory("F00B4R"); std::cout << foo->foobar << std::endl; return 0; } // http://ideone.com/dhZTRF
  • 104. CONSTANT EXPRESSIONS •Use constexpr whenever possible •Objects: const + value known during compilation. •Functions: return constexpr result if called with constexpr args.
  • 105. CONSTANT EXPRESSIONS •constexpr objects constexpr auto foo = 42; // Okay! constexpr auto bar = foo; // Okay! bar = 11; // error: assignment of read-only variable 'bar' std::array<unsigned, foo> qux; // Okay! static_assert(foo == 42, "Foo must be 42!"); // Okay! int size = 42; constexpr auto foobar = size; // error: the value of 'size' is not usable in a constant expression
  • 106. CONSTANT EXPRESSIONS •constexpr objects vs const objects int bar = 42; const auto foo = bar; // okay: may be initialized at runtime std::array<unsigned, foo> qux; // error: foo is not a constant expression •All constexpr objects are const, but not all const objects are constexpr. •If you need compile-time value, then constexpr is the proper tool.
  • 107. CONSTANT EXPRESSIONS •constexpr objects vs const objects int bar = 42; const auto foo = bar; // okay: may be initialized at runtime std::array<unsigned, foo> qux; // error: foo is not a constant expression •All constexpr objects are const, but not all const objects are constexpr. •If you need compile-time value, then constexpr is the proper tool.
  • 108. CONSTANT EXPRESSIONS •constexpr objects rules: •may any literal type including: •float point types •char literals •pointer literals •literal obejcts •requires no storage declaration •constexpr parameters not allowed: void foo(constexpr int bar) // error: could not be constexpr { std::array<int, bar> qux; }
  • 109. CONSTANT EXPRESSIONS •constexpr functions: •execute during compile-time, and return constexpr, only: •the arguments are constexpr •the result is used in constexpr context •execute (perphaps not) during compile-time, when: •the arguments are constexpr •the result is not used in constexpr context •execute at run-time when there is 1 or more non-constexpr arguments. •no need to overload, •constexpr function take both constexpr and non-constexpr args.
  • 110. CONSTANT EXPRESSIONS constexpr int half_of(double value) noexcept { return value / 2; } int main() { constexpr auto foo = half_of(42); // compile-time call static_assert(foo == 21, "Ops!"); auto bar = half_of(12); // runtime call assert(bar == 6); const auto foobar = half_of(12); // compile-time call static_assert(foobar == 6, "Ops!"); auto qux = 74; auto couse = half_of(qux); // runtime call assert(couse == 37); return 0; }
  • 111. CONSTANT EXPRESSIONS •constexpr functions rules: •its return type shall be a literal type; and •its parameter types shall be literal types; and •its function-body shall be a compound-statement of the form: { return expression; } where expression is a potential constant expression; and •every implicit conversion used in converting expression to the function return type shall be one of those allowed in a constant expression. •constexpr functions and constexpr constructors are implicitly inline •a constexpr function shall not be virtual
  • 112. CONSTANT EXPRESSIONS •constexpr functions: constexpr int square(int x) { return x * x; } // OK constexpr long long_max() { return 2147483647; } // OK constexpr int abs(int x) { return x < 0 ? -x : x; } // OK constexpr void f(int x) { /* ... */ } // error: return type is void constexpr int prev(int x) { return --x; } // error: use of decrement constexpr int g(int x, int n) { int r = 1; // error: body not just “return expr’’ while (--n > 0) r *= x; return r; }
  • 113. CONSTANT EXPRESSIONS •Relaxed constexpr restrictions C++14: •any declaration, except: •static •thread_local •conditional branching statements if and switch; •any looping statement, including range-based for; •change the value of an object if the lifetime of that object began within the constant expression function; constexpr int pow(int base, int exp) noexcept { auto result = 1; for (int i = 0; i < exp; ++i) result *= base; return result; }
  • 114. CONSTANT EXPRESSIONS •Things to remember: •constexpr objects are const and are initialized with values known during compilation. •constexpr functions can produce compile-time results when called with arguments whose values are known during compilation. •constexpr objects and functions may be used in a wider range of contexts than non-constexpr objects and functions. •constexpr is part of an object’s or function’s interface.
  • 115. THE ELEMENTS OF C++11 •How to improve my skills: •books: •more effective c++ and modern effective c++; Scott Meyers •a tour of C++; bjarne stormtrooper •web •https://isocpp.org/ •http://scottmeyers.blogspot.com/ •http://herbsutter.com/ •http://erdani.com/ •http://cppcon.org/ •http://cppcast.com/

Notes de l'éditeur

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115