Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does the compiler want a trailing return-type?
    text
    copied!<p>I was playing around with an adapter for using range-based for-loops to iterate in reverse. (I did not know about the boost adapter ("adaptor") for that purpose. I am a big believer in not re-inventing the wheel if it's a free wheel I have already downloaded.)</p> <p>What puzzles me is why VC++ 2012 is not happy unless I use trailing return-types in the code that follows:</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; template&lt;class Fwd&gt; struct Reverser { const Fwd &amp;fwd; Reverser&lt;Fwd&gt;(const Fwd &amp;fwd_): fwd(fwd_) {} auto begin() -&gt; decltype(fwd.rbegin()) const { return fwd.rbegin(); } auto end() -&gt; decltype(fwd.rend()) const { return fwd.rend(); } }; template&lt;class Fwd&gt; Reverser&lt;Fwd&gt; reverse(const Fwd &amp;fwd) { return Reverser&lt;Fwd&gt;(fwd); } int main() { using namespace std; const string str = ".dlrow olleH"; for(char c: reverse(str)) cout &lt;&lt; c; cout &lt;&lt; endl; } </code></pre> <p>When I tried the following, I got the errors, "error C2100: illegal indirection," and "error C2228: left of '.rbegin' must have class/struct/union". What am I missing?</p> <pre><code>template&lt;class Fwd&gt; struct Reverser { const Fwd &amp;fwd; Reverser&lt;Fwd&gt;(const Fwd &amp;fwd_): fwd(fwd_) {} decltype(fwd.rbegin()) begin() const { return fwd.rbegin(); } decltype(fwd.rend()) end() const { return fwd.rend(); } }; </code></pre> <p>UPDATE: In light of the discussion about a "this" pointer, I tried another tack. Look Ma, no this! And it compiles fine. I do believe that, rightly or wrongly, VC++ is not aware of that this.</p> <pre><code>template&lt;class Fwd&gt; struct Reverser { const Fwd &amp;fwd; Reverser&lt;Fwd&gt;(const Fwd &amp;fwd_): fwd(fwd_) {} decltype(((const Fwd*)0)-&gt;rbegin()) begin() const { return fwd.rbegin(); } decltype(((const Fwd*)0)-&gt;rend()) end() const { return fwd.rend(); } }; </code></pre> <p>UPDATE 2: Submitted to MS: <a href="https://connect.microsoft.com/VisualStudio/feedback/details/765455/vc-2012-compiler-refuses-decltype-return-spec-for-member-function" rel="nofollow noreferrer">https://connect.microsoft.com/VisualStudio/feedback/details/765455/vc-2012-compiler-refuses-decltype-return-spec-for-member-function</a></p> <p>UPDATE 3: This problem is fixed as of VC++ 2015. Thanks, Microsoft person.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload