So, after writing about a bunch of things that _won't_ be in C++14,
let me summarize what _will_ be. Here are the highlights:
* Return type deduction
In C++11, we have decltype() and late-specified return types
(return types written after the argument list so they can
mention arguments). These were often combined to write code
like this:
auto function(A a, B b) -> decltype(expr)
{
return expr;
}
Having to repeat 'expr' is rather unfortunate, and C++14
fixes that. You can now write:
auto function(A a, B b)
{
return expr;
}
It works even if the function body contains multiple
statements, control flow, etc. The only restriction is
that if there are multiple return expressions, they
must all have the same type.
* Improved lambdas
* Generic lambdas
These are lambdas with 'auto' arguments which acts as
template parameters rather than concrete types:
// This lambda can be called with arguments of any type.
[](auto a, auto b){ return a > b; }
Implementation-wise, they are just local structures with
a templated call operator (while ordinary lambdas are
local structures with a non-templated call operator).
* Init-captures
These allow you to declare new variables in the capture-list
rather than just capture existing variables from the outer
scope. The new variables can be initialized in any way,
including by move, something that wasn't possible before.
std::unique_ptr<Foo> p = ...;
...
// No way to capture 'p' in C++11. Can't capture it by
// value because it's not copyable; don't want to capture
// it by reference because the reference could become
// dangling if the lambda is passed out of the function.
// In C++14 we can do this:
[newp = move(p)](...){...}
* Variable templates
Up until now, only functions and classes/structures could be
templates. Now, variables can be too.
* More powerful constexpr
Up until now, constexpr functions were limited to containing
a single return expression. Now they can contain multiple
statements, loops, and so on. Basically, they can now be
written in an imperative style rather than a functional
style. They still can't modify non-local state.
* Improved literals
* Binary literals: 0b00101010101
* Digit separators: int x = 1'000'000; // don't ask
* Standard user-defined literals
C++11 introduced user-defined literals. C++14 intoduces
some standard ones:
"foo"s // std::string literal
30s // std::chrono::seconds literal
Yes, they are both 's'. C++ knows when to use which.
Other durations have them too, so you can write things
like
1h + 10min + 30s
Botond
_______________________________________________
dev-platform mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-platform