Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Oh, Baby, Give Me ++Chance

Andy Gross
December 03, 2013

Oh, Baby, Give Me ++Chance

A Timid Defense Of C++(11)

Andy Gross

December 03, 2013
Tweet

More Decks by Andy Gross

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

  4. 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

    View Slide

  5. 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

    View Slide

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

    View Slide

  7. 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_;!
    };!

    View Slide

  8. 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;!
    }!

    View Slide

  9. 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_;!
    };!

    View Slide

  10. 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 = …;!
    }

    View Slide

  11. 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!
    }!

    View Slide

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

    View Slide

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

    View Slide

  14. 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;!
    }!

    View Slide

  15. 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])

    View Slide

  16. 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

    View Slide

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

    View Slide

  18. 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?

    View Slide