1 / 5

My Favorite C++ 10-Liner

My Favorite C++ 10-Liner. Herb Sutter. How Easy Is C++, Really? 13 +5 LOC Example. # include "cinder/app/ AppBasic.h " #include "cinder/dx/ dx.h " #include <vector> using namespace ci; using namespace ci::app ;

aria
Download Presentation

My Favorite C++ 10-Liner

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. My Favorite C++ 10-Liner Herb Sutter

  2. How Easy Is C++, Really? 13+5LOC Example • #include "cinder/app/AppBasic.h"#include "cinder/dx/dx.h"#include <vector>using namespace ci;using namespace ci::app; • class MyApp: public AppBasic {std::vector<Vec2f> points;public: void mouseDrag( MouseEvente ) { points.push_back(e.getPos()); } • void draw() { dx::clear( Color( 0.1f, 0.1f, 0.15f ) ); dx::color( 1.0f, 0.5f, 0.25f );  dx::begin( GL_LINE_STRIP ); for( auto& e : points ) dx::vertex(e); dx::end(); }}; • CINDER_APP_BASIC( MyApp, RendererDx )

  3. My Favorite C++ 10-Liner A complete reference-counted object cache C++98 • map operator[] auto-insertion C++11 • auto • mutex, lock_guard • Thread-safe fn statics • shared_ptr • weak_ptr • Thread-safe .lock() • shared_ptr<widget> get_widget( int id ) { • static map<int, weak_ptr<widget>> cache;staticmutex m; • lock_guard<mutex> hold(m);autosp = cache[id].lock();    if (!sp) cache[id] = sp = load_widget(id);    return sp; • } assertion failure: checksum(linecnt)

  4. Since I Had 3 Lines Left Over… Meyers Singleton C++11 • Thread-safe fn statics Bonus: Does destruction! • widget& instance() { • static widget w; • return w; • }

  5. My Favorite C++ 10-Liner

More Related