Skip to content

Latest commit

 

History

History
204 lines (160 loc) · 1.95 KB

small_stuff.md

File metadata and controls

204 lines (160 loc) · 1.95 KB

Nested Namespaces

C++14 C++17
   namespace A {
      namespace B {
         namespace C {
            struct Foo { };
            //...
         }
      }
   }
   namespace A::B::C {
      struct Foo { };
      //...
   }

Single Param static_assert

C++14 C++17
static_assert(sizeof(short) == 2, "sizeof(short) == 2")
static_assert(sizeof(short) == 2)
Output
static assertion failure: sizeof(short) == 2

Inline Variables

C++14 C++17
// foo.h
extern int foo;

// foo.cpp
int foo = 10;
// foo.h
inline int foo = 10;
C++14 C++17
// foo.h
struct Foo {
   static int foo;
};

// foo.cpp
int Foo::foo = 10;
// foo.h
struct Foo {
   static inline int foo = 10;
};

Guaranteed Copy Elision

C++17
// header <mutex>
namespace std
{
   template <typename M>
   struct lock_guard
   {
      explicit lock_guard(M & mutex);
      // not copyable, not movable:
      lock_guard(lock_guard const & ) = delete;
      //...
   }
}

// your code
lock_guard<mutex> grab_lock(mutex & mtx)
{
   return lock_guard<mutex>(mtx);
}

mutex mtx;

void foo()
{
   auto guard = grab_lock(mtx);
   /* do stuff holding lock */
}