Home > Article > Backend Development > Why Does GCC Reject Explicit Specialization Outside of a Namespace?
GCC Inconsistency with Standard for Explicit Specialization in Non-Namespace Scope
The C standard explicitly allows explicit template specializations to be declared in any scope where the corresponding primary template can be defined. However, the behavior of GCC deviates from this standard. This discrepancy has been observed in recent versions of GCC, where explicit specializations declared outside of a namespace fail to compile.
To illustrate the issue, consider the following code:
template<typename T> struct Widget { template<typename U> void foo(U) { } template<> void foo(int*) { } };
While this code compiles successfully in Clang, it encounters an error in GCC:
error: explicit specialization in non-namespace scope 'struct Widget<T>'
This error is inconsistent with the C standard, which explicitly allows explicit specialization in non-namespace scope. According to paragraph 2 of [temp.expl.spec] in the C standard:
"An explicit specialization may be declared in any scope in which the corresponding primary template may be defined."
This deviation from the standard constitutes a potential bug in GCC. To report this issue, follow these steps:
By reporting this issue, you can contribute to the improvement of GCC and ensure that it fully adheres to the C standard.
The above is the detailed content of Why Does GCC Reject Explicit Specialization Outside of a Namespace?. For more information, please follow other related articles on the PHP Chinese website!