Instead of implementing all the C++ 11 features first and then targeting C++ 14, the VC++ team have taken an approach where they will implement both in parallel. This will allow them to implement popular C++ 14 features ahead of less popular C++ 11 features. Either way, at some point, they will have a release which completely supports C++ 11 and C++ 14. One very commonly requested C++ 14 feature is auto/decltype(auto) return types and the CTP supports both.
Here’s an example, where using it saves some typing and the code looks cleaner.
auto Foo() { map<int, vector<pair<int, string>>> vec; return vec; }
Of course, that is subjective, and some people may feel that the auto is confusing there. It is more useful with templates where the return type would normally need a decltype.
template<class T1, class T2> auto Foo(T1 a, T2 b) { return a + b; }
You can use it with class methods, and even do forward declarations.
class C { public: auto Foo(); }; auto C::Foo() { return 10; }
Multiple returns are also supported, and that’s also for your lambdas.
auto Bar() { C c; if (c.Foo() < 10) { return 3.3; } return 1.7; }
Using decltype(auto) gives you even more flexibility. Consider this code.
int F1() { return 5; } int i = 5; int& F2() { return i; }
Now if you call these as follows.
auto f1 = F1(); // int - correct auto f2 = F2(); // int - inaccurate, lost original type decltype(auto) f3 = F1(); // int - correct decltype(auto) f4 = F2(); // int& - correct
It’s the same with templates, where you can save on a decltype.
struct T { int& Foo(int& i) { return i; } double Foo(double& d) { return d; } template<class T> decltype(auto) NewWay(T& t) { return Foo(t); } template<class T> auto OldWay(T& t) -> decltype(Foo(t)) { return Foo(t); } };
That’s a simple example, but library writers would appreciate this as it significantly simplifies their code and makes it way easier to understand when going through someone else’s code.
Intriguing write-up. Might you direct me to more of your work.