Professional Documents
Culture Documents
Going Native Highlights: New C++ Style
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
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];
decltype Operator
decltype(num) anotherNum; anotherNum = num; decltype(notes[1]) note1; note1 = notes[1];
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?
Pessimization
void f(std::shared_ptr<widget> w);
Uniform Initialization
QPC_SlideRefsListPtr slideRefs{ ChNEW QPC_SlideRefsList }; const char USERNAME[]{"USER"}; std::vector<CES::Int_64> v{0, 1, 2, 5};
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);
nullptr Keyword
char *pc = nullptr; int *pi = nullptr; bool b = nullptr; int i = nullptr;
void foo(char*) {}
foo(nullptr);
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
10
Move Semantics
Vector constructors
vector(const vector& other); vector(vector&& other);
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
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; }
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";
14
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));
15
};
16
18
constexpr Specifier
constexpr int fact(int x) { return x == 1 ? 1 : x * fact(x - 1); }
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); }
20
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
22
Questions???
23