为什么函数参数中的别名模板与它引用的嵌套类型不匹配?

H.W*_*Wei 8 c++ g++ compiler-bug template-aliases

这段代码编译

\n
template<typename T>\nstruct A {\n    struct Inner {} inner;\n    void foo(Inner in);\n};\n\ntemplate<typename T>\nvoid A<T>::foo(typename A<T>::Inner in) {\n\n}\n\nint main() {\n    A<int> a;\n    a.foo(a.inner);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

typename A<T>::Inner如果我更改为别名模板,则不会

\n
template<typename T>\nusing AInner = typename A<T>::Inner;\n\ntemplate<typename T>\nvoid A<T>::foo(AInner<T> bar) {\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

编译器抱怨说error: no declaration matches \xe2\x80\x98void A<T>::foo(AInner<T>)\xe2\x80\x99

\n

我不太熟悉别名模板的规则。他们不是同一类型吗?

\n
\n

编辑:\n这是完整的演示:

\n

// 你好.cpp

\n
template<typename T>\nstruct A {\n    struct Inner {} inner;\n    void foo(Inner in);\n};\n\ntemplate<typename T>\nusing AInner = typename A<T>::Inner;\n\ntemplate<typename T>\nvoid A<T>::foo(AInner<T> in) {\n\n}\n\nint main() {\n    A<int> a;\n    a.foo(a.inner);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我编译它g++ (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0

\n

输出:

\n
hello.cpp:11:6: error: no declaration matches \xe2\x80\x98void A<T>::foo(AInner<T>)\xe2\x80\x99\n   11 | void A<T>::foo(AInner<T> in) {\n      |      ^~~~\nhello.cpp:4:10: note: candidate is: \xe2\x80\x98void A<T>::foo(A<T>::Inner)\xe2\x80\x99\n    4 |     void foo(Inner in);\n      |          ^~~\nhello.cpp:2:8: note: \xe2\x80\x98struct A<T>\xe2\x80\x99 defined here\n    2 | struct A {\n      |\n
Run Code Online (Sandbox Code Playgroud)\n

Fed*_*dor 0

你的代码是正确的。这是 g++ 的一个已知错误,请参阅https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101789

其他编译器可以使用此代码,在线演示:https ://gcc.godbolt.org/z/5Pv9KW8vj