llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

clang fails to parse type alias in explicit calling of template member function with `->template `

Open Rush10233 opened this issue 2 years ago • 2 comments

Please consider the following code:

template <typename T>
struct A {
    int a() {
        return 42;
    }
};

template <typename T>
struct B {
    
    using MUT_1 = A<T>;
    int b(MUT_1 *p) {
        return p->template MUT_1::a();
    }
};

int main() {  
  A<int> a;
  B<int> b;
    return b.b(&a);
}

Clang rejects it by complaining that:

<source>:13:28: error: a template argument list is expected after a name prefixed by the template keyword [-Wmissing-template-arg-list-after-template-kw]
   13 |         return p->template MUT_1::a();
      |                            ^
<source>:13:33: error: expected ';' after return statement
   13 |         return p->template MUT_1::a();
      |                                 ^
      |                                 ;
<source>:13:28: error: no member named 'MUT_1' in 'A<int>'
   13 |         return p->template MUT_1::a();
      |                ~           ^
<source>:20:14: note: in instantiation of member function 'B<int>::b' requested here
   20 |     return b.b(&a);

It seems the compiler does not correctly understand the type alias expression of using MUT_1 = A<T>;. Directly using A<T> without setting up an alias won't trigger any error. Additionally, GCC also accepts this code.

Please check https://godbolt.org/z/oYre1GEh3

Rush10233 avatar Jun 28 '24 07:06 Rush10233

@llvm/issue-subscribers-clang-frontend

Author: None (Rush10233)

Please consider the following code:
template &lt;typename T&gt;
struct A {
    int a() {
        return 42;
    }
};

template &lt;typename T&gt;
struct B {
    
    using MUT_1 = A&lt;T&gt;;
    int b(MUT_1 *p) {
        return p-&gt;template MUT_1::a();
    }
};

int main() {  
  A&lt;int&gt; a;
  B&lt;int&gt; b;
    return b.b(&amp;a);
}

Clang rejects it by complaining that:

&lt;source&gt;:13:28: error: a template argument list is expected after a name prefixed by the template keyword [-Wmissing-template-arg-list-after-template-kw]
   13 |         return p-&gt;template MUT_1::a();
      |                            ^
&lt;source&gt;:13:33: error: expected ';' after return statement
   13 |         return p-&gt;template MUT_1::a();
      |                                 ^
      |                                 ;
&lt;source&gt;:13:28: error: no member named 'MUT_1' in 'A&lt;int&gt;'
   13 |         return p-&gt;template MUT_1::a();
      |                ~           ^
&lt;source&gt;:20:14: note: in instantiation of member function 'B&lt;int&gt;::b' requested here
   20 |     return b.b(&amp;a);

It seems the compiler does not correctly understand the type alias expression of using MUT_1 = A&lt;T&gt;;. Directly using A&lt;T&gt; without setting up an alias won't trigger any error. Additionally, GCC also accepts this code.

Please check https://godbolt.org/z/oYre1GEh3

llvmbot avatar Jun 28 '24 14:06 llvmbot

clang is correct here see: https://github.com/llvm/llvm-project/issues/94194

Actually simply removing template makes this work: https://godbolt.org/z/KffbhcT11

Please reopen if I am missing something.

shafik avatar Jul 02 '24 22:07 shafik