1

If a function template is marked as deleted, is it allowed to explicitly instantiate it as in the example:

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

template int foo(int);

Clang and GCC allows it, while MSVC prints the error:

error C2280: 'int foo<int>(int)': attempting to reference a deleted function

Demo: https://gcc.godbolt.org/z/49hfqnr4f

Which compiler is right here?

Fedor
  • 17,146
  • 13
  • 40
  • 131

1 Answers1

3

Clang and GCC are right. MSVC is probably referring to the following rule ([dcl.fct.def.delete]/2):

A program that refers to a deleted function implicitly or explicitly, other than to declare it, is ill-formed.

An explicit instantiation definition is a declaration, so it is allowed.

Although, to be fair, it's not clear what "refers" means in this context, so there is some room for language lawyering. But it's clear that, in general, the mere instantiation of a template to produce a deleted function definition is allowed. [temp.inst]/3.2 also mentions the implicit instantiation of deleted member functions that occurs when class templates are implicitly instantiated. If instantiating a templated entity to produce a deleted function definition were ill-formed, it wouldn't be possible to use class templates like std::atomic that have a deleted copy constructor. Your program merely does explicitly what happens implicitly under those circumstances.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • Thanks! Just to clarify that I properly understand *an explicit instantiation definition is a declaration*. If one removes the body of function template and requests its explicit instantiation, shall it be an error as in this example https://gcc.godbolt.org/z/WMs5deGha? – Fedor Dec 10 '21 at 08:15
  • @Fedor I'm not sure what that has to do with the answer. But see http://eel.is/c++draft/temp.explicit#7 – Brian Bi Dec 10 '21 at 13:30