The following code should fail to compile due to ambiguity (the non-template case properly fails): template<typename> void foo(void); template<> void foo<int>(void); namespace { template<> void foo<int>(void) { return; } } int main(int, char **) { foo<int>(); }
It should fail to compile because you are specializing a template which was never declared, not because it is ambiguous. namespace { template<> void foo<int>(void) { return; } } That is a specialization, but there is no matching primary template. This fails due to ambiguous overloads: template<typename> void foo(void); template<> void foo<int>(void); namespace { template<class> void foo(void) { return; } }
This fails: template<typename> void foo(void); template<> void foo<int>(void); namespace { template<> void foo<int>(void) { return; } } int main(int, char **) { foo<int>(); } ttt.cc:6: error: specialization of 'template<class> void foo()' in different namespace ttt.cc:2: error: from definition of 'template<class> void foo()' But if a valid specialization already exists, the one in the anon namespace is not rejected. That's a bug.
Oops, I've just realised I pasted the wrong code in comment 2, I meant to say that this fails: template<typename> void foo(void); namespace { template<> void foo<int>(void) { return; } } template<> void foo<int>(void); int main(int, char **) { foo<int>(); } i.e. if the invalid specialization comes before the valid specialization, it's rejected. But in the original example, where the invalid specialization comes later, it's incorrectly accepted. The same error should be given in both cases: the specialization in the anon namespace does not match any primary template declaration.
It doesn't even depend on unnamed namespaces! This is also accepted: template<typename> void foo(void); template<> void foo<int>(void); namespace xxx { template<> void foo<int>(void) { return; } } I've adjusted the summary accordingly.
incomplete patch posted to http://gcc.gnu.org/ml/gcc-patches/2010-10/msg00054.html
(In reply to Jonathan Wakely from comment #5) > incomplete patch posted to > http://gcc.gnu.org/ml/gcc-patches/2010-10/msg00054.html adding the "patch" keyword
Fixed by r7-4488
Dup of bug 56480. *** This bug has been marked as a duplicate of bug 56480 ***