Slide 1

Slide 1 text

Oh, Baby, Give Me ++Chance ! A Timid Defense of C++(11) Andy Gross <@argv0> Chief Architect, Basho Technologies

Slide 2

Slide 2 text

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” ! - Bjarne Stroustrup

Slide 3

Slide 3 text

“Within C++, there is a much smaller and cleaner language struggling to get out.” ! - Bjarne Stroustrup

Slide 4

Slide 4 text

C++ : The most misunderstood language • Cargo-culted snark: • templates are weird! • omg boost takes so long to compile • i’ll have to manually manage every memory allocation • gahhh derp derp herrrrrrrrp // hacker news

Slide 5

Slide 5 text

The Best C++ Feature • RAII: “Resource acquisition is initialization” • Crappy acronym, simple concept • Acquire resources in constructors, release them in destructors • Deterministic destruction does the rest • Java try-with-resources is an ugly hack

Slide 6

Slide 6 text

RAII struct lock_guard! {! lock_guard(mutex mtx)! : mtx_(mtx)! {! acquire_mutex(mtx_);! }! ! ~lock_guard()! {! release_mutex(mtx_);! }! ! mutex mtx_;! };!

Slide 7

Slide 7 text

RAII struct my_object! {! void do_something()! {! lock_guard g(mtx_);! do_something_with_state_that_might_throw(state_);! // mtx_ is released regardless of how function exits! }! mutex mtx_;! state state_;! };!

Slide 8

Slide 8 text

RAII struct my_other_obj! {! my_other_obj()! : myobj_(make_shared()) {}! ! void do_something()! {! // acquires lock in my_object! myobj_.do_something()! }! ! // destroyed automaticaly when object goes out of scope! shared_ptr myobj_;! };! ! int main(void)! {! auto o = std::make_shared();! o->do_something();! return 0;! }!

Slide 9

Slide 9 text

RAII template ! struct locked_queue ! {! typedef lock_guard lock_guard;! ! locked_queue()! : q_(make_shared()) {}! ! void produce(const T& item)! {! lock_guard lock(mtx_);! q_.push(item);! }! ! void consume(T& item)! {! lock_guard lock(mtx_);! item = q_.front();! q_.pop();! }! ! private:! shared_ptr> q_;! mutex mtx_;! };!

Slide 10

Slide 10 text

Literal Containers void cpp03()! {! // the old crappy way! vector my_ints;! for (int i=0;i<10;i++)! my_ints.push_back(i);! for (vector::iterator it=my_ints.begin(); it != my_ints.end(); ++it)! cout << *it << endl;! }! ! void cpp11()! {! // how’d your literal initialization game come up?! vector my_ints = {1,2,3,4,5,6,7,8,9,10};! // ayo java peep this:! for (auto i: my_ints)! cout << i << endl;! }! ! void literal_maps()! {! // maps and tuples too! map my_map = {{"foo", "bar"}, {"baz", "quux"}};! ! tuple = …;! }

Slide 11

Slide 11 text

Lambdas! void lambda_lambda_lambda();! {! auto my_simple_lambda = [](){ cout << "hello, world\n"; };! // prints "hello, world” ! my_simple_lambda();! int i = 0;! // captures i by copy! // prints 2, but i is unchanged in outer scope;! auto my_copy_capture_lambda = [i]() { i+=1; cout << i; };! my_copy_capture_lambda();! ! // captures i by copy! // prints i+y, i in outer scope is incremented by y! auto my_ref_capture_lambda = [&i](int y) { i+=y; cout << i; };! my_ref_capture_lambda();! ! // [&] = capture all by reference;! // [=] = capture all by copy! }!

Slide 12

Slide 12 text

Threads void thread_func()! {! a_long_calculation();! print_result();! }! ! ! int main(void)! {! thread t(thread_func);! t.join();! };!

Slide 13

Slide 13 text

Futures int main(void)! {! auto fut = async([](){ return a_long_calculation_returning_int(); });! // read hacker news! int x = fut.get();! };!

Slide 14

Slide 14 text

Constexpr // evaluated at compile time! constexpr int f(int x) { return x > 1 ? x * f(x - 1) : 1; }! ! int main(void)! {! // evaluated at compile time! cout << f(10) << endl;! ! int i = get_an_int_at_runtime();! ! // evaluated at runtime! cout << f(i) << endl;! }!

Slide 15

Slide 15 text

Other Goodies • nullptr : explicit null type, not convertible/ comparable to/with integral/boolean types • final/override keywords • static assertions • type traits • atomic types (atomic_[u]int[32|64])

Slide 16

Slide 16 text

Better Toolchain • clang vastly superior to gcc • comes with static analysis, AST introspection • even writing analysis plugins is reasonably simple • lldb > gdb too, once you relearn all the commands

Slide 17

Slide 17 text

Try It At Home • Assuming recent clang: • CXXFLAGS = “-std=c++11 -stdlib=libc++”

Slide 18

Slide 18 text

Conclusions • The combination of C++11 and the Clang toolchain makes C++ worth checking out again • Or maybe I just have Stockholm Syndrome? • … your thoughts or questions?