0

I have a general template function that is =deleted, and a specialization for int

template <typename> void f() = delete;

template <>
void f<int>() { }

My goal is to have a function template that errors at compile-time (not link-time) when a specialization is not provided for the template arguments. f<int>() should succeed and f<double>() should error. While the above works with gcc, it fails with clang with error: redefinition of 'f'. If I remove the =delete it compiles fine, but then f<double>() doesn't fail until link time.

Is the above valid? Is the specialization of a deleted template a redefinition or should the specialization be another candidate for overload resolution.

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • This might just be a clang bug. Although when reading through N3936 looking for relevant text I was surprised to find "When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation". Looks like that text made it into C++14 too – M.M Dec 01 '15 at 21:16
  • instead of specializing can you overload it like [this](http://stackoverflow.com/questions/32716589/force-function-to-be-called-only-with-specific-types)? – NathanOliver Dec 01 '15 at 21:17
  • @NathanOliver I would if I could. The template type isn't related to any of the arguments. – Ryan Haining Dec 01 '15 at 21:18
  • In this code sample the specialization should have `inline` if the function body is present (otherwise it may lead to ODR violation) – M.M Dec 01 '15 at 21:20
  • @M.M right, but the error from that is: `error: inline declaration of 'f' follows non-inline definition` which is less helpful in determining what's being confused here. – Ryan Haining Dec 01 '15 at 21:22
  • That message doesn't make any sense, which leads me to believe clang is bugged in the case of specializing a deleted function – M.M Dec 01 '15 at 21:26
  • [Here](https://llvm.org/bugs/show_bug.cgi?id=17537) is the 2 year old bug report. The proposed "solution" at the end is particularly neat. – vsoftco Dec 01 '15 at 21:30

0 Answers0