return-type-deduction

Is it possible to ignore the trailing return type feature of c++11 in favor of the function return type deduction feature of c++14?

独自空忆成欢 提交于 2019-12-05 00:17:08
When I skip the return type of an expression The following code in C++11 : auto function(X x, Y y) -> decltype(x + y) { return x + y; } Is equal to the following code in C++14 : decltype(auto) function(X x, Y y) { return x + y; } But additionally it is possible to deduce the return type without decltype rules in C++14 : auto function() { return 0; } When I know what the return type is exactly The following code in C++11 : auto function() -> int { return 0; } Is equal to the following code in C++03 : int function() { return 0; } A strange example that should never happen The following code in C

Template friend function and return type deduction

偶尔善良 提交于 2019-12-04 19:05:56
问题 Note: This question is really close to Return type deduction for in-class friend functions, but I did not find the answer to my problem there. Tested with clang 3.4 with std=c++1y and clang 3.5 with std=c++14 and std=c++1z This code compiles: #include <iostream> template<class T> class MyClass { public: MyClass(T const& a) : impl(a) {} template<class T0, class T1> friend auto // requires operator+(T0,T1) exists operator+(MyClass<T0> const& a, MyClass<T1> const& b) { return MyClass<decltype(a

Return type deduction with a private member variable

北城余情 提交于 2019-12-04 16:02:11
问题 As was explained in this Q&A yesterday, both g++ 4.8 and Clang 3.3 correctly complain about the code below with an error like "'b_' was not declared in this scope" #include <iostream> class Test { public: Test(): b_(0) {} auto foo() const -> decltype(b_) // just leave out the -> decltype(b_) works with c++1y { return b_; } private: int b_; }; int main() { Test t; std::cout << t.foo(); } Moving the private section to the top of the class definition eliminates the error and prints 0. My

Return type deduction for class methods? C++1y

别来无恙 提交于 2019-12-04 03:30:18
问题 Is return type deduction allowed for member functions in c++14, or only for free functions? I ask because I sort of implicitly assumed it would work, but in gcc 4.8.1 I get an internal compiler error("in gen_type_die_with_usage"). First time I have ever gotten such a cryptic error like that, so I am a bit skeptical; and I know they have changed the spec since then. For clarity this works for me: auto foo() {return 5;} but this doesn't: class Bar{ auto baz() {return 5;} } Is this allowed in

Legitimate uses of the trailing return type syntax as of C++14

有些话、适合烂在心里 提交于 2019-12-03 13:47:58
Is there actually any reason to use the following syntax anymore : template<typename T> auto access(T& t, int i) -> decltype(t[i]) { return t[i]; } Now that we can use : template<typename T> decltype(auto) access(T& t, int i) { return t[i]; } The trailing return type syntax now seems a little redundant? Deduced return types are not SFINAE friendly. This overload will simply drop out of the overload set if t[i] is invalid: template<typename T> auto access(T& t, int i) -> decltype(t[i]) { return t[i]; } Whereas this overload will not, leading to a hard error: template<typename T> decltype(auto)

Template friend function and return type deduction

不羁的心 提交于 2019-12-03 12:41:45
Note: This question is really close to Return type deduction for in-class friend functions , but I did not find the answer to my problem there. Tested with clang 3.4 with std=c++1y and clang 3.5 with std=c++14 and std=c++1z This code compiles: #include <iostream> template<class T> class MyClass { public: MyClass(T const& a) : impl(a) {} template<class T0, class T1> friend auto // requires operator+(T0,T1) exists operator+(MyClass<T0> const& a, MyClass<T1> const& b) { return MyClass<decltype(a.impl+b.impl)>{a.impl + b.impl}; } T getImpl() const { return impl; } private: T impl; }; int main() {

CRTP and c++1y return type deduction

感情迁移 提交于 2019-12-03 11:37:35
I was recently playing with CRTP when I came across something that surprised me when used with c++1y functions whose type is deduced. The following code works: template<typename Derived> struct Base { auto foo() { return static_cast<Derived*>(this)->foo_impl(); } }; struct Derived: public Base<Derived> { auto foo_impl() -> int { return 0; } }; int main() { Derived b; int i = b.foo(); (void)i; } I assumed that the return type from Base<Derived>::foo was a decltype of the expression returned, but if I modify the functio foo like this: auto foo() -> decltype(static_cast<Derived*>(this)->foo_impl(

Return type deduction for class methods? C++1y

时光怂恿深爱的人放手 提交于 2019-12-01 17:55:18
Is return type deduction allowed for member functions in c++14, or only for free functions? I ask because I sort of implicitly assumed it would work, but in gcc 4.8.1 I get an internal compiler error("in gen_type_die_with_usage"). First time I have ever gotten such a cryptic error like that, so I am a bit skeptical; and I know they have changed the spec since then. For clarity this works for me: auto foo() {return 5;} but this doesn't: class Bar{ auto baz() {return 5;} } Is this allowed in the draft standard? Yes the standard should allow it according to the paper n3582 . Here is an example

Using functions that return placeholder types defined in another translation unit

岁酱吖の 提交于 2019-12-01 06:06:57
问题 I'm having some trouble understanding how the C++14 extension of the auto type-specifier described in N3638 can possibly be implemented, and what, exactly, is allowed. Specifically, one of the changes to the standard says, If the declared return type of the function contains a placeholder type, the return type of the function is deduced from return statements in the body of the function, if any. It is easy enough to see how this works when body of the function is in the same file as the

Variadic template recursive return type deduction compilation error

社会主义新天地 提交于 2019-11-30 20:13:13
Why the code below does not compile? template <typename T> T sum(T t){ return t; } template <typename T, typename ...U> auto sum(T t, U... u) -> decltype(t + sum(u...)) { return t + sum(u...); } int main() { sum(1, 1.5, 2); } Compilation error: error: no matching function for call to ‘sum(int, double, int)’ sum(1, 1.5, 2); What would be a good workaround to implement this feature? Here we forward the work to helper types: namespace details { template<class...Ts> struct sum_t {}; template<class T> struct sum_t<T> { T operator()(T t)const{ return std::forward<T>(t); } }; template<class T, class.