範囲ベースのforループを使用して逆方向に反復するためのアダプターを試してみました。(私はその目的のためのブーストアダプター(「アダプター」)について知りませんでした。私は、それがすでにダウンロードしたフリーホイールである場合、ホイールを再発明しないことを大いに信じています。)
私を困惑させるのは、次のコードで末尾のreturn-typeを使用しない限り、VC++2012が満足できない理由です。
#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;
}
次のことを試してみると、「エラーC2100:不正な間接参照」および「エラーC2228:「。rbegin」の左側にはclass / struct/unionが必要です」というエラーが発生しました。私は何が欠けていますか?
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」ポインターに関する議論に照らして、私は別の方法を試しました。ほら、これじゃない!そしてそれはうまくコンパイルされます。私は、正しいか間違っているかにかかわらず、VC++はこれを認識していないと信じています。
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(); }
};
更新3:この問題はVC ++ 2015で修正されています。ありがとう、マイクロソフトの担当者。