trailing-return-type

auto, decltype(auto) and trailing return type

风格不统一 提交于 2020-01-23 11:06:32
问题 Is there a difference between: template <class T> constexpr decltype(auto) f(T&& x) -> decltype(std::get<0>(std::forward<T>(x))) { return std::get<0>(std::forward<T>(x)); } and: template <class T> constexpr auto f(T&& x) -> decltype(std::get<0>(std::forward<T>(x))) { return std::get<0>(std::forward<T>(x)); } and if so, what is it, and which one should I use for perfect forwarding? 回答1: Trailing return type should only be used with auto The point of decltype(auto) vs auto is to distinguish the

Does a placeholder in a trailing-return-type override an initial placeholder?

∥☆過路亽.° 提交于 2020-01-14 08:27:09
问题 g++ appears to accept any combination of auto and decltype(auto) as initial and trailing return types: int a; auto f() { return (a); } // int auto g() -> auto { return (a); } // int auto h() -> decltype(auto) { return (a); } // int& decltype(auto) i() { return (a); } // int& decltype(auto) j() -> auto { return (a); } // int decltype(auto) k() -> decltype(auto) { return (a); } // int& However, clang rejects j and k , saying: error: function with trailing return type must specify return type

Class member visibility in member function declaration signature

核能气质少年 提交于 2020-01-13 18:30:28
问题 Why does this work: template <typename A> struct S { A a; template <typename B> auto f(B b) -> decltype(a.f(b)) { } }; But this does not ( a and f swapped places): template <typename A> struct S { template <typename B> auto f(B b) -> decltype(a.f(b)) { } A a; }; saying that a is not declared in that scope (inside decltype) but adding explicit this-> makes it work. 回答1: template <typename A> struct S { A a; template <typename B> auto f(B b) -> decltype(a.f(b)) { } }; This works because within

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?

戏子无情 提交于 2020-01-02 00:56:14
问题 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

Can i use auto or decltype instead trailing return type?

狂风中的少年 提交于 2020-01-01 01:12:29
问题 I find trailing return type so easy to define the return of a function that returns a complicated types e.g: auto get_diag(int(&ar)[3][3])->int(&)[3]{ // using trailing return type static int diag[3]{ ar[0][0], ar[1][1], ar[2][2] }; return diag; } auto& get_diag2(int(&ar)[3][3]){ // adding & auto because otherwise it converts the array to pointer static int diag[3]{ ar[0][0], ar[1][1], ar[2][2] }; return diag; } int main(){ int a[][3]{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; decltype(get_diag(a))

Point of declaration for variadic template

穿精又带淫゛_ 提交于 2019-12-30 09:52:27
问题 At what point is a variadic template considered "declared"? This compiles under clang++ 3.4, but not under g++ 4.8.2. template <typename T> const T &sum(const T &v) { return v; } template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)); template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)) { return v + sum(params...); } int main() { sum(1, 2, 3); } Apparently g++ won't match

Point of declaration for variadic template

霸气de小男生 提交于 2019-12-30 09:52:16
问题 At what point is a variadic template considered "declared"? This compiles under clang++ 3.4, but not under g++ 4.8.2. template <typename T> const T &sum(const T &v) { return v; } template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)); template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)) { return v + sum(params...); } int main() { sum(1, 2, 3); } Apparently g++ won't match

Point of declaration for variadic template

丶灬走出姿态 提交于 2019-12-24 10:47:29
问题 At what point is a variadic template considered "declared"? This compiles under clang++ 3.4, but not under g++ 4.8.2. template <typename T> const T &sum(const T &v) { return v; } template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)); template <typename T, typename ... Ts> auto sum(const T &v, const Ts & ... params) -> decltype(v + sum(params...)) { return v + sum(params...); } int main() { sum(1, 2, 3); } Apparently g++ won't match

Is 'auto a_class::f(…) -> T const' ambiguous in the grammar?

旧街凉风 提交于 2019-12-23 06:47:10
问题 How should the below member function prototype be interpreted in C++11? class C { public: auto f(...) -> T const; } It would seem to me that it could either be a const member function of the class C, or a non-const member function which returns a const value of type T. I know I could just write the function as T const f(...); or T f(...) const; However, I want to be consistent with how I declare functions, so I decided to use the new C++11 auto f(...) -> RetType way everywhere. 回答1: The

Using this and attributes in member function trailing return types?

早过忘川 提交于 2019-12-22 05:32:06
问题 In this answer I gave, it made sense to use this and the attribute of the class _arg in the trailing return type as part of the decltype expression. It's possible to do without, but inconvenient. Neither clang 3.0 (see below) nor gcc 4.5.2 accepted it though. #include <iostream> class MyClass { public: MyClass(int i): _arg(i) {} template <typename F> auto apply(F& f) -> decltype(f(_arg)) { return f(_arg); } template <typename F> auto apply(F& f) -> decltype(f(*this, _arg)) { return f(*this,