Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Going Native Highlights

New C++ Style

Agenda
New C++ core language features
auto and decltype smart pointers uniform initialization nullptr rvalue references lambda functions range-based loops user-defined literals and few others

Quickoffice Proprietary and Confidential - Not for Disclosure

auto Type
const auto num = QOSTR("0123456789"); const auto* numPtr = num.c_str(); auto loader = boost::bind(&PptXArchive::DiagramDataLoader, this, _1, _2); std::vector<CES::QPD_DOM::QPD_NotesMasterPtr> notes; auto note = notes[0];

Base* p = new Derived();

Quickoffice Proprietary and Confidential - Not for Disclosure

decltype Operator
decltype(num) anotherNum; anotherNum = num; decltype(notes[1]) note1; note1 = notes[1];

template<typename U, typename T>


auto foo(T& t) -> decltype(std::make_shared<U>(t));

Quickoffice Proprietary and Confidential - Not for Disclosure

Really Smart Pointers


std::unique_ptr for unique ownership. It can be stored in a

standard container
std::shared_ptr and std::weak_ptr for shared ownership.

Shared pointer is thread safe. std::auto_ptr is deprecated!!! Raw pointers and references still can be used but... But is the pointer really needed?

Quickoffice Proprietary and Confidential - Not for Disclosure

Pointer Passing to Functions


Default:
void f(widget* w); void f(widget& w);

Explicit about shared ownership


void f(std::shared_ptr<widget>& w);

Pessimization
void f(std::shared_ptr<widget> w);

Quickoffice Proprietary and Confidential - Not for Disclosure

Uniform Initialization
QPC_SlideRefsListPtr slideRefs{ ChNEW QPC_SlideRefsList }; const char USERNAME[]{"USER"}; std::vector<CES::Int_64> v{0, 1, 2, 5};

int i{42}; vs int i = 42;


m_renderer.MoveTo(QO_Dpoint(x, y)); vs m_renderer.MoveTo({x, y});

Quickoffice Proprietary and Confidential - Not for Disclosure

std::initializer_list

struct SequenceClass { SequenceClass(std::initializer_list<int> list); }; SequenceClass some_var = {1, 4, 5, 6}; void foo(std::initializer_list<float> l);

foo({1.0f, -3.45f, -0.4f});


std::vector<int> v(10); vs std::vector<int> v{10};

There is no way to make an instance of std::initializer_list


Quickoffice Proprietary and Confidential - Not for Disclosure 8

nullptr Keyword
char *pc = nullptr; int *pi = nullptr; bool b = nullptr; int i = nullptr;

void foo(char*) {}

foo(nullptr);

0 still can be cast to pointer No need for NULL

Quickoffice Proprietary and Confidential - Not for Disclosure

Rvalue References
Rvalue references help in implementing move semantics and in perfect forwarding X&& rvalue reference; X& lvalue reference Overload for X&& and X& mostly makes sense in copy constructor(move constructor) and in assignment operator Other functions can be overloaded too Rvalue reference can be either rvalue or lvalue

Quickoffice Proprietary and Confidential - Not for Disclosure

10

Move Semantics
Vector constructors
vector(const vector& other); vector(vector&& other);

Big objects can be returned by value


std::vector<int> foo() {

return {1, 2, 32, 11};


}

Quickoffice Proprietary and Confidential - Not for Disclosure

11

Perfect Forwarding
template<typename T, typename Arg> shared_ptr<T> factory(Arg arg) { return shared_ptr<T>(new T(arg)); }

arg can value, reference or const reference but none of these is perfect

Quickoffice Proprietary and Confidential - Not for Disclosure

12

Lambda functions
[capture](arguments)->return-type {body}

Capture by value(=) or by reference(&) Return-type can be omitted this is always captured by value Lambdas have same class members access as enclosing function
auto a = [flag](int x, int y) { return flag ? x + y : x - y; }

Quickoffice Proprietary and Confidential - Not for Disclosure

13

Lambda functions
std::function<int(int)> f0 = [](int) {return 1;}; auto decltype(f0) for(auto& f : fa) f1 = [](int x) {return x;}; fa = { std::cout << f(2) << "\n";

f0, f1, [](int x) {return x * x;} };

auto foo = [](int x) { ++x;};


void(*foo_ptr)(int) = foo;

Quickoffice Proprietary and Confidential - Not for Disclosure

14

begin()/end() Functions and Range-based

Loops
int a[]{1, 4, 2, 10, 3}; std::vector<int> v{1, 4, 2, 10, 3}; for (auto& d : v) { total += d; } std::sort(std::begin(v), std::end(v)); std::sort(std::begin(a), std::end(a));

Quickoffice Proprietary and Confidential - Not for Disclosure

15

Object Construction Delegation


struct A { A(int i, int j) : m_i{i}, m_j{j} {} A() : A(1, 0) {} private: int m_i; int m_j;

};

C++11 considers an object constructed once any constructor finishes

Quickoffice Proprietary and Confidential - Not for Disclosure

16

override and final


struct Base { virtual void foo(int) {} virtual void bar() final {} }; struct Derived : public Base {

void foo(double) override {} // Error


void bar() {} } // Error

Class also can be final


Quickoffice Proprietary and Confidential - Not for Disclosure 17

default and delete


struct NonCopyable { NonCopyable& operator=(const NonCopyable&) = delete; NonCopyable(const NonCopyable&) = delete; NonCopyable() = default; void f(double d);

template<class T> void f(T) = delete;


};

Quickoffice Proprietary and Confidential - Not for Disclosure

18

constexpr Specifier
constexpr int fact(int x) { return x == 1 ? 1 : x * fact(x - 1); }

More computations in compile time without any templates

Quickoffice Proprietary and Confidential - Not for Disclosure

19

User-Defined Literals
CES::QO_Color operator"" _r(int r) { return CES::QO_Color(r, 0, 0); } CES::QO_Color operator"" _g(int g) { return CES::QO_Color(0, g, 0); }

CES::QO_Color operator"" _b(int b) {


return CES::QO_Color(0, 0, b); } constexpr CES::QO_Color c = 12_r + 30_g + 100_b;

Quickoffice Proprietary and Confidential - Not for Disclosure

20

Lets Refactor in New C++ Style

Quickoffice Proprietary and Confidential - Not for Disclosure

21

References
http://channel9.msdn.com/Events/GoingNative/GoingNativ e-2012 http://en.wikipedia.org/wiki/C%2B%2B11 http://thbecker.net/articles/rvalue_references/section_01. html http://gcc.gnu.org/projects/cxx0x.html http://clang.llvm.org/cxx_status.html

Quickoffice Proprietary and Confidential - Not for Disclosure

22

Questions???

Quickoffice Proprietary and Confidential - Not for Disclosure

23

You might also like