Clang fails to merge default template arguments across multiple declarations of a member class template within a class template
Similar to #135442, but for class templates.
Clang rejects this code, while MSVC, GCC, and EDG accept it (godbolt):
template <class>
struct Foo {
template <class T = int>
struct Bar;
template <class T>
struct Bar {};
using MyBar = Bar<>;
};
template struct Foo<void>;
According to [temp.param]/18, the default arguments for Bar should be merged, so this code should be valid.
Notably, the issue disappears if:
- the enclosing class is non-template, or
- the default argument is placed on the definition rather than the declaration (godbolt).
@llvm/issue-subscribers-clang-frontend
Author: None (NylteJ)
Clang rejects this code, while MSVC, GCC, and EDG accept it (godbolt):
template <class>
struct Foo {
template <class T = int>
struct Bar;
template <class T>
struct Bar {};
using MyBar = Bar<>;
};
template struct Foo<void>;
According to [[temp.param]/18](https://eel.is/c++draft/temp.param#18), the default arguments for Bar should be merged, so this code should be valid.
Notably, the issue disappears if:
- the enclosing class is non-template, or
- the default argument is placed on the definition rather than the declaration (godbolt).
CC @Endilll
On a quick look, this indeed sounds like a bug on our side. AST is expectedly capturing the relationship between the template parameters in declarations of Foo::Bar, so the problem is elsewhere:
| | |-ClassTemplateDecl 0x37483318 <line:3:5, line:4:12> col:12 Bar
| | | |-TemplateTypeParmDecl 0x37483168 <line:3:15, col:25> col:21 class depth 1 index 0 T
| | | | `-TemplateArgument type 'int'
| | | | `-BuiltinType 0x3742ddf0 'int'
| | | `-CXXRecordDecl 0x37483268 <line:4:5, col:12> col:12 struct Bar
| | |-ClassTemplateDecl 0x374836b0 prev 0x37483318 <line:6:5, line:7:17> col:12 Bar
| | | |-TemplateTypeParmDecl 0x37483568 <line:6:15, col:21> col:21 class depth 1 index 0 T
| | | | `-TemplateArgument type 'int'
| | | | |-inherited from TemplateTypeParm 0x37483168 'T'
| | | | `-BuiltinType 0x3742ddf0 'int'
| | | `-CXXRecordDecl 0x41a2c8d8 prev 0x41a2c708 <line:7:5, col:17> col:12 struct Bar definition
<...>
Wild guess: maybe the default argument is somehow dependent, and we forget to instantiate it, leading to failing to consider it for Bar<>?
@mizvekov
We too are observing this issue. Any progress on this?
My guess is that we don't instantiate all redeclarations of a member template.