Skip to content

Instantly share code, notes, and snippets.

@prehistoricpenguin
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save prehistoricpenguin/9775899 to your computer and use it in GitHub Desktop.

Select an option

Save prehistoricpenguin/9775899 to your computer and use it in GitHub Desktop.

c++11 - the new standard

auto and init list

for (const auto x : { 1, 2, 3}}) cout << x <<  endl;

In-class member initializers

class A {
public:
    int a = 0xff;
};

class A {
public:
    A() {}
private:
    hashingFunction hash_algorithm{"MD5"};
    std::string s{"Contructor run"};
};

swap - use rvalue-reference

tempalte <typename T>
void swap(T& a, T& b) {
    T tmp = std::move(a);
    a = std::move(b);
    b = std::move(tmp);
}

memory - pointers

// unique_ptr
X* f() {
    unique_ptr<X> ptr(new X());
    // do something - maybe throw an exception
    
    return ptr.release();
}

unique_ptr<X> f() {
    unique_ptr<X> ptr(new X());
    // ...
    
    return ptr;
}
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment