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

[Clang] Suboptimal diagnostics on missing template arguments in the type of explicit instantiation of variable template

Open MagentaTreehouse opened this issue 1 year ago • 2 comments

The following C++17 code is accepted by GCC and MSVC, but rejected by Clang.

template <class T>
struct A { A(T) {} };

template <class>
A a{0};

template A a<int>;

Output:

<source>:7:10: error: 'auto' variable template instantiation is not allowed
    7 | template A a<int>;
      |          ^

See https://compiler-explorer.com/z/PerPhffKG.

MagentaTreehouse avatar Feb 18 '25 04:02 MagentaTreehouse

@llvm/issue-subscribers-clang-frontend

Author: None (MagentaTreehouse)

The following C++17 code is accepted by GCC and MSVC, but rejected by Clang.
template &lt;class T&gt;
struct A { A(T) {} };

template &lt;class&gt;
A a{0};

template A a&lt;int&gt;;

Output:

&lt;source&gt;:7:10: error: 'auto' variable template instantiation is not allowed
    7 | template A a&lt;int&gt;;
      |          ^

See https://compiler-explorer.com/z/PerPhffKG.

llvmbot avatar Feb 18 '25 04:02 llvmbot

It looks like the code is invalid per [dcl.type.class.deduct]:

If a placeholder for a deduced class type appears as a decl-specifier in the decl-specifier-seq of an initializing declaration ([dcl.init]) of a variable, the declared type of the variable shall be cv T, where T is the placeholder. [...]

A placeholder for a deduced class type can also be used in the type-specifier-seq in the new-type-id or type-id of a new-expression ([expr.new]), as the simple-type-specifier in an explicit type conversion (functional notation), or as the type-specifier in the parameter-declaration of a template-parameter ([temp.param]). A placeholder for a deduced class type shall not appear in any other context.

But Clang's diagnostics can be improved, because the code doesn't say auto anywhere.

MagentaTreehouse avatar Feb 19 '25 00:02 MagentaTreehouse