警告:模板在不同名称空间中的特化 [英] warning: specialization of template in different namespace
问题描述
通过以下代码,我得到警告:
With the following piece of code I get the warning:
警告:"template< class _Iterator>不同名称空间中的struct std :: iterator_traits [-fpermissive]
template<> class std::iterator_traits<Token_ptr>{
public:
typedef Word difference_type;
typedef Word value_type;
typedef Token_ptr pointer;
typedef Word& reference ;
typedef std::bidirectional_iterator_tag iterator_category ;
};
尽管一切正常,但是没有人知道确切的含义以及发出警告的原因.(g ++发出警告,而clang ++则不发出警告.)
While everything works correctly, does any body know what exactly means and why is issued the warning. ( g++ issues the warning while clang++ doesn't ).
推荐答案
假设您正在C ++ 11模式下编译此代码(因为clang没有给出警告),并且此专业化位于全局名称空间中,那么没有任何问题与您的代码.这是一个g ++错误.§14.7.3[temp.expl.spec]/p2:
Assuming that you are compiling this in C++11 mode (since clang gave no warning) and that this specialization is in the global namespace, then there's nothing wrong with your code. It's a g++ bug. §14.7.3 [temp.expl.spec]/p2:
显式专业化应在封闭的名称空间中声明专业模板.一个显式的专业,其 declarator-id 不合格,应在模板的最近封闭名称空间中声明,或者如果该名称空间是内联的,则声明该声明(7.3.1),其封闭名称空间集中的任何名称空间.这样的声明也可以是定义.如果声明不是定义,以后可以定义专业化(7.3.1.2).
An explicit specialization shall be declared in a namespace enclosing the specialized template. An explicit specialization whose declarator-id is not qualified shall be declared in the nearest enclosing namespace of the template, or, if the namespace is inline (7.3.1), any namespace from its enclosing namespace set. Such a declaration may also be a definition. If the declaration is not a definition, the specialization may be defined later (7.3.1.2).
全局命名空间是一个包含专用模板的命名空间",并且您的 declarator-id 用 std ::
限定,因此第二句话不适用.解决方法是,您可以执行 cdhowie 的答案所建议的-即打开 namespace std
阻止并将专业化放置在那里.
The global namespace is a "namespace enclosing the specialized template", and your declarator-id is qualified with std::
, so the second sentence doesn't apply. As a workaround, you can do what cdhowie's answer suggests - i.e., opening a namespace std
block and putting the specialization there.
请参见 CWG问题374 和 GCC错误56480 .
这篇关于警告:模板在不同名称空间中的特化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!