title: Using this and attributes in member function trailing return types?
body: <p>In this <a href="https://stackoverflow.com/questions/9017139/is-it-possible-to-base-the-template-specialization-on-the-presence-or-absence-of/9018068#9018068">answer</a> I gave, it made sense to use <code>this</code> and the attribute of the class <code>_arg</code> in the trailing return type as part of the <code>decltype</code> expression. It's possible to do without, but inconvenient.</p>
  
  <p>Neither clang 3.0 (see below) nor <a href="http://ideone.com/qYJ4r" rel="nofollow noreferrer">gcc 4.5.2</a> accepted it though.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class MyClass {
  public:
    MyClass(int i): _arg(i) {}
  
    template &lt;typename F&gt;
    auto apply(F&amp; f) -&gt; decltype(f(_arg)) {
      return f(_arg);
    }
  
    template &lt;typename F&gt;
    auto apply(F&amp; f) -&gt; decltype(f(*this, _arg)) {
      return f(*this, _arg);
    }
  
  private:
    int _arg;
  };
  
  struct Id {
    template &lt;typename V&gt;
    V operator()(V v) const { return v; }
  };
  
  struct ComplexId {
    template &lt;typename C, typename V&gt;
    V operator()(C const&amp;, V v) { return v + 1; }
  };
  
  int main() {
    Id id; ComplexId complex;
  
    MyClass c(0);
  
    std::cout &lt;&lt; c.apply(id) &lt;&lt; " " &lt;&lt; c.apply(complex) &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>clang 3.0 says:</p>
  
  <pre><code>$ clang++ -std=c++11 -Weverything test.cpp
  test.cpp:8:38: error: use of undeclared identifier '_arg'
        auto apply(F&amp; f) -&gt; decltype(f(_arg)) {
                                       ^
  test.cpp:8:45: error: type name requires a specifier or qualifier
        auto apply(F&amp; f) -&gt; decltype(f(_arg)) {
                                              ^
  test.cpp:8:45: error: C++ requires a type specifier for all declarations
        auto apply(F&amp; f) -&gt; decltype(f(_arg)) {
                            ~~~~~~~~          ^
  test.cpp:8:7: error: 'auto' return without trailing return type
        auto apply(F&amp; f) -&gt; decltype(f(_arg)) {
        ^
  test.cpp:13:39: error: invalid use of 'this' outside of a nonstatic member function
        auto apply(F&amp; f) -&gt; decltype(f(*this, _arg)) {
                                        ^
  test.cpp:13:52: error: type name requires a specifier or qualifier
        auto apply(F&amp; f) -&gt; decltype(f(*this, _arg)) {
                                                     ^
  test.cpp:13:52: error: C++ requires a type specifier for all declarations
        auto apply(F&amp; f) -&gt; decltype(f(*this, _arg)) {
                            ~~~~~~~~                 ^
  test.cpp:13:7: error: 'auto' return without trailing return type
        auto apply(F&amp; f) -&gt; decltype(f(*this, _arg)) {
        ^
  8 errors generated.
  </code></pre>
  
  <p>Hum... not so great.</p>
  
  <p>However, the support of C++11 is hacky at best in most compilers and I could not find specific restrictions mentionned in the Standard (n3290).</p>
  
  <p>In the comments, Xeo suggested that it might have been a defect in the Standard...</p>
  
  <p><strong>So, is this allowed or not ?</strong></p>
  
  <p><em>Bonus: and do more recent versions of clang / gcc support this ?</em></p>