Title
Can template functions and classes be declared in the same scope?
Status
cd6
Section
Clause [13] [temp]
Submitter
John Spicer

Created on 1999-04-28.00:00:00 last changed 28 months ago

Messages

Date: 2020-11-15.00:00:00

[Accepted at the November, 2020 meeting as part of paper P1787R6 and moved to DR at the February, 2021 meeting.]

According to Clause 13 [temp] paragraph 5,

Except that a function template can be overloaded either by (non-template) functions with the same name or by other function templates with the same name (13.10.4 [temp.over] ), a template name declared in namespace scope or in class scope shall be unique in that scope.
_N4868_.6.4.10 [basic.scope.hiding] paragraph 2 agrees that only functions, not function templates, can hide a class name declared in the same scope:
A class name (11.3 [class.name] ) or enumeration name (9.7.1 [dcl.enum] ) can be hidden by the name of an object, function, or enumerator declared in the same scope.
However, 6.4 [basic.scope] paragraph 4 treats functions and template functions together in this regard:
Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,
  • they shall all refer to the same entity, or all refer to functions and function templates; or
  • exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same object or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden

John Spicer: You should be able to take an existing program and replace an existing function with a function template without breaking unrelated parts of the program. In addition, all of the compilers I tried allow this usage (EDG, Sun, egcs, Watcom, Microsoft, Borland). I would recommend that function templates be handled exactly like functions for purposes of name hiding.

Martin O'Riordan: I don't see any justification for extending the purview of what is decidedly a hack, just for the sake of consistency. In fact, I think we should go further and in the interest of consistency, we should deprecate the hack, scheduling its eventual removal from the C++ language standard.

The hack is there to allow old C programs and especially the 'stat.h' file to compile with minimum effort (also several other Posix and X headers). People changing such older programs have ample opportunity to "do it right". Indeed, if you are adding templates to an existing program, you should probably be placing your templates in a 'namespace', so the issue disappears anyway. The lookup rules should be able to provide the behaviour you need without further hacking.

History
Date User Action Args
2022-08-19 07:54:33adminsetstatus: drwp -> cd6
2021-02-24 00:00:00adminsetstatus: accepted -> drwp
2020-12-15 00:00:00adminsetstatus: open -> accepted
1999-04-28 00:00:00admincreate