Timothy Liu
Timothy Liu

Reputation: 406

unusable-partial-specialization error with Clang by default

I am just programming with C++ templates, but Clang gave me an error by default.

The code below:

#include <type_traits>
#include <utility>

template <int... vals>
struct int_seq {};

template <typename>
struct pop_back;

template <int... vals, int val>
struct pop_back<int_seq<vals..., val>>
    : std::enable_if<true, std::pair<std::integral_constant<int, val>, int_seq<vals...>>> {};

Can be compiled successfully with GCC and MSVC. But Clang will give an -Wunusable-partial-specialization error:

$ clang++ -std=c++17 -c main.cpp
error: class template partial specialization contains template parameters that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]

However, if we specify the -Wno-unusable-partial-specialization option, it can be compiled successfully:

$ clang++ -std=c++17 -c main.cpp -Wno-unusable-partial-specialization

Diff view in godbolt: https://gcc.godbolt.org/z/sd6MW5MTT

So which behavior is correct?

Upvotes: 1

Views: 173

Answers (1)

user12002570
user12002570

Reputation: 1

So which behavior is correct?

The program is ill-formed as per temp.class.spec.match#3 as the template parameter cannot be deduced:

If the template arguments of a partial specialization cannot be deduced because of the structure of its template-parameter-list and the template-id, the program is ill-formed.

[Example 3 :

template <int I, int J> struct A {};
template <int I> struct A<I+5, I*2> {}; // error

end example]

(emphasis mine)


Note that if you don't actually use the template then the program is ill-formed no diagnostic required. But once you start using it, it will be ill-formed and a diagnostic should be issued in that case. Here is a demo that actually uses the template and makes the program ill-formed.

Upvotes: 2

Related Questions