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

Clang fails to merge default template arguments across multiple declarations of a member class template within a class template

Open NylteJ opened this issue 1 year ago • 6 comments

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).

NylteJ avatar Sep 27 '25 05:09 NylteJ

@llvm/issue-subscribers-clang-frontend

Author: None (NylteJ)

Similar to #135442, but for class templates.

Clang rejects this code, while MSVC, GCC, and EDG accept it (godbolt):

template &lt;class&gt;
struct Foo {
    template &lt;class T = int&gt;
    struct Bar;

    template &lt;class T&gt;
    struct Bar {};

    using MyBar = Bar&lt;&gt;;
};

template struct Foo&lt;void&gt;;

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).

llvmbot avatar Sep 27 '25 13:09 llvmbot

CC @Endilll

shafik avatar Sep 29 '25 16:09 shafik

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<>?

Endilll avatar Sep 29 '25 17:09 Endilll

@mizvekov

zyn0217 avatar Oct 07 '25 15:10 zyn0217

We too are observing this issue. Any progress on this?

vrukesh avatar Jan 14 '26 13:01 vrukesh

My guess is that we don't instantiate all redeclarations of a member template.

mizvekov avatar Feb 12 '26 17:02 mizvekov