C++11 abuse
I’d like to use the new features in C++11 and the GCC extensions in anger. What are they?
rvalue references:
- Use?
Generalised constant expressions:
constexpr
to mark a function/constructor as compile time constant- non-void return
- can’t declare variables, declare types
- single return
- non-integrals are now OK:
constexpr double foo = ...
POD:
- Wider than before
- trivial: can be statically initialised, can copy via memcpy()
- standard layout: C compatible
- No virtual functions, base classes
- Same access control
Initialiser lists:
- Class can have a std::initializer_list
constructor - Foo foo = {1, 2, 3};
- bar({4, 5, 6})
- std::vector std::string v = { “x”, “y” }
Uniform initialisation:
- Not sure?
struct Foo { int a; int b; } ... Foo get_bar() { return ( 5, 6 ); }
Type inference:
- Awesome!
auto foo = wacky_template_type::yeah<int, bar>...
auto foo = 5;
decltype(some_variable) some_other;
Range based for:
for (int &x : some_int_array) {}
- Also works with iterators, begin()/end()
Anonymous functions:
[](int arg1, int arg2) { return arg1 + arg2; }
Trailing return type:
- Just like Go
auto foo(int x, int y) -> int;
auto foo(int x, int y) -> decltype(x+y);
- Used as x, y are now defined
Constructors:
- Delegation:
Foo(int x) ...; Foo() : Foo(42) {}
- Better than default values as the default gets spread across all callers
- Base class constructor inheritance:
Derived : Base { using Base::Base; }
- Member initialisation:
int value = 5;
will be called by all constructors explicit Constructor...
to skip
Explicit override:
- Mark that you intended to override a base function
virtual void do_it() override;
Final classes and functions:
struct Base final {}
: can’t derrive fromvirtual void the_last() final;
Null pointer constant:
- No more zero, no more NULL: just
nullptr
- No ambiguity with int args
Fancy enums:
enum class Foo : char { X, Y };
- Gives Foo::X
- Explicit storage type
Extended templates:
- Typedef for a partial template specialisation
using TypedefName = SomeType<OtherType, Second, 5>;
using OtherType = void (*)(double);
is the same as typedef (*OtherType)(double)
Unrestricted unions:
- union can hold a type with a non trivial constructor
String literals:
- u8"I’m in UTF-8 and a const char and a random \u2012 character"
- u"I’m in UTF-16 and a const char16_t"
- U"I’m in UTF-32 and a const char32_t"
- R"(I’m a raw string)"
- R"xy(So am I)xy"
Explicitly deleted/defaulted members:
- Explicit default constructor
class Foo { Foo() = default; }
- No default copy constructor
class Foo { Foo(const Foo&) = delete; }
- No implicit cast
class Foo { void f(double); void f(int) = delete; }
Misc:
>>
closes templates. Don’t need> >
explicit
on conversion operators and constructors to prevent implicit conversion to other types- Variadic templates?
- User defined literals:
Type variable = 1234_suffix;
- But when are they evaluated? compile, start, or runtime?
- static_assert
- sizeof on members:
struct Foo { int bar; }; sizeof(Foo::bar);
- alignof(), alignas()
Standard library:
- std::thread
- std::lock_guard, std::unique_lock
- Futures, promises, and async
- std::tuples