When a function template specializes in another namespace, GCC and clang disagree

See the following code:

namespace ns { template <typename T> void func() {} } template <> void ns::func<int>() {} int main() {} 

While clang 3.6 (C ++ 14) compiles fine, GCC 5.2 (C ++ 14) produces the following error

 main.cpp:9:20: error: specialization of 'template<class T> void ns::func()' in different namespace [-fpermissive] void ns::func<int>() {} ^ main.cpp:4:6: error: from definition of 'template<class T> void ns::func()' [-fpermissive] void func() {} ^ 

So what does the standard say about this? Who is right?

+5
source share
1 answer

What does the standard ( n3797 ) mean ?

 14.7.3 Explicit Specialization [temp.expl.spec] 

Explicit specialization is declared in the namespace spanning the specialized pattern. Explicit specialization An ad ID is not qualified; it is declared in the nearest enclosing namespace of the template, or, if the namespace is inline (7.3.1), any namespace from its encompassing namespace. Such a declaration may also be a definition. If the declaration is not a definition, specialization may be determined later (7.3.1.2).

Verdict Your specialization is an explicit specialization, and it is qualified, which means that the fragment is legal. So the behavior shown by clang is correct.


Relevant error report for gcc:

+6
source

Source: https://habr.com/ru/post/1233429/


All Articles