Bug 30857 - [4.1/4.2/4.3 regression] accepts both explicit instantiation and explicit specialization, duplicate explicit instantiations, etc.
Summary: [4.1/4.2/4.3 regression] accepts both explicit instantiation and explicit spe...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.0
: P2 normal
Target Milestone: 4.1.3
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2007-02-19 11:19 UTC by Benjamin Kosnik
Modified: 2008-01-24 17:36 UTC (History)
5 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2007-08-06 15:01:27


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Benjamin Kosnik 2007-02-19 11:19:22 UTC
I thought g++ used to warn for this:

template <typename T>
class foo { };

template<> class foo<int> { };

template class foo<int>;

Where the error was something informative like "one may not explicitly specialize and explicitly instantiate" blah blah blah

However, mainline currently does not do this.

From 14.7

-5- No program shall explicitly instantiate any template more than once, both explicitly instantiate and explicitly specialize a template, or specialize a template more than once for a given set of template-arguments. An implementation is not required to diagnose a violation of this rule.

So, this is not required by the standard. As a QoI issue, however, this would be  beneficial. 

Here's why I wanted this: if one is doing API testing, and the API requires explicit specializations, than one possible negative test is to explicitly instantiate the required specialization and get an error. 

Otherwise, I suppose another neg test is to redefine the specialization. This is also not required to give an error, but current g++ does (redefinition error). 

Note also, no diagnostic for duplicate explicit instantiations:

template <typename T>
class foo { };

template class foo<int>;
template class foo<int>;


Either of these or all may be a warning regressions (if my mind is not playing tricks on me.)

I have not tested with other compilers, or previous g++ versions.

Consistent behavior for all three cases would be appreciated.
Comment 1 Wolfgang Bangerth 2007-03-09 04:20:37 UTC
We got a warning in gcc2.95, but it has apparently already disappeared in
gcc3.2.x.

W.
Comment 2 Simon Martin 2007-09-23 19:49:59 UTC
I've done tests with the current mainline and I wonder if this PR is still "valid"...

We report an error for the following three cases:

1. Multiple specializations:
   template <typename T> class A {};
   template<> class A<int> {};
   template<> class A<int> {};

2. Multiple explicit instantiations:
   template <typename T> class B {};
   template class B<int>;
   template class B<int>;

3. Specialization following an explicit instantiation:
   template <typename T> class D {};
   template class D<int>;
   template<> class D<int> {};

The fact that we accept an explicit instantiation after a specialization is due to DR 259.
Comment 3 Paolo Carlini 2008-01-24 16:42:15 UTC
Benjamin, can you have a look to the resolution of DR 259:

  http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#259

I believe Simon is basically right...
Comment 4 Benjamin Kosnik 2008-01-24 17:18:17 UTC
I have verified that all three of simon's test cases now error on mainline, as expected. Hurray!!! So, this can be closed as fixed, I guess.

Note 4.1.2 also errors, so maybe this was a result of some temporary fluctuation in mainline.

I would like to make sure that Simon's test cases make it into the g++ testsuite, so that this doesn't reappear in the future. (But am unsure about the state of g++ testsuite and don't want duplicates, so I defer.)
Comment 5 Paolo Carlini 2008-01-24 17:36:41 UTC
Excellent, indeed. I also checked stock 4.2.1 and current 4_2-branch.