Search code examples
c++gccclangnvcccompiler-bug

Can you `= delete` a templated function on a second declaration?


Consider the following code:

template <typename T> int foo();
template <typename T> int foo() = delete;

is this valid C++11?

  • GCC (9.1) says: Yes!
  • clang (8.0) says: No!
  • nvcc (9.2) says: No!
  • MSVC (19.20) says: Yes! (in C++14 mode, it doesn't support C++11.)

... see it all on GodBolt.

so which compilers are right and which compilers are s@#$%e ? :-)


Solution

  • GCC and MSVC have a bug.

    [dcl.fct.def.delete]

    4 ... A deleted definition of a function shall be the first declaration of the function or, for an explicit specialization of a function template, the first declaration of that specialization...

    Which I believe stands for instantiated declarations and definitions too. Since referring to a deleted function is a hard error, it must be declared as deleted asap.