Why does the compiler want to get a return type?

I played with an adapter to use for-loops to iterate in reverse order. (I did not know about the boost adapter (“adapter”) for this purpose. I really believe that I am not reinventing the wheel if it is a free wheel that I have already downloaded.)

What puzzles me is why VC ++ 2012 is not happy if I do not use trailing return-types in the following code:

#include <string>
#include <iostream>

template<class Fwd>
struct Reverser {
    const Fwd &fwd;
    Reverser<Fwd>(const Fwd &fwd_): fwd(fwd_) {}
    auto begin() -> decltype(fwd.rbegin()) const { return fwd.rbegin(); } 
    auto end() ->   decltype(fwd.rend())   const { return fwd.rend(); } 
};

template<class Fwd>
Reverser<Fwd> reverse(const Fwd &fwd) { return Reverser<Fwd>(fwd); }

int main() {
    using namespace std;
    const string str = ".dlrow olleH";
    for(char c: reverse(str)) cout << c;
    cout << endl;
}

When I tried the following, I got errors, "error C2100: illegal indirection" and "error C2228: to the left of" .rbegin "should be class / structure / union". What am I missing?

template<class Fwd>
struct Reverser {
    const Fwd &fwd;
    Reverser<Fwd>(const Fwd &fwd_): fwd(fwd_) {}
    decltype(fwd.rbegin()) begin() const { return fwd.rbegin(); } 
    decltype(fwd.rend())   end() const { return fwd.rend(); } 
};

: "this" . , ! . , , , V++ .

template<class Fwd>
struct Reverser {
    const Fwd &fwd;
    Reverser<Fwd>(const Fwd &fwd_): fwd(fwd_) {}
    decltype(((const Fwd*)0)->rbegin()) begin() const { return fwd.rbegin(); } 
    decltype(((const Fwd*)0)->rend())   end()   const { return fwd.rend(); } 

};

2: MS: https://connect.microsoft.com/VisualStudio/feedback/details/765455/vc-2012-compiler-refuses-decltype-return-spec-for-member-function

3: ​​ V++ 2015. , Microsoft.

+5
1

OP: V++ . V++ 2015 .

, , V++, , : , : 5.1 [expr.prim.general] 12 , id-, -. , :

id- .

decltype(expr) . , 3 9.3.1 [class.mfct.non-static] , this :

id (5.1), (5.2.5) (5.3.1), X , (5.1.1), (3.4) id - C, id- C X X, id- (5.2.5), (* this) (9.3.2) . .

" ", . , this . , ,

decltype(fwd.rbegin()) begin() const;

. ,

decltype(static_cast<Reverser<Fwd> const*>(0)->fwd.rbegin()) begin() const;

- , .

+5

All Articles