Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 931
einpoklum Avatar asked Jun 01 '19 18:06

einpoklum


People also ask

When to use delete vs delete []?

delete is used for one single pointer and delete[] is used for deleting an array through a pointer.

What does the delete keyword do when applied on a function declaration?

Using the delete operator on an object deallocates its memory.

Will delete operator works for normal function?

= delete can be used for any function, in which case it is explicitly marked as deleted and any use results in a compiler error.

What does delete [] mean in C++?

Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator. New operator is used for dynamic memory allocation which puts variables on heap memory.


1 Answers

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.

like image 176
StoryTeller - Unslander Monica Avatar answered Sep 21 '22 23:09

StoryTeller - Unslander Monica