gpt4 book ai didi

c++ - 警告 : specialization of template in different namespace

转载 作者:可可西里 更新时间:2023-11-01 16:26:45 26 4
gpt4 key购买 nike

通过以下代码我得到了警告:

warning: specialization of ‘template<class _Iterator> struct std::iterator_traits’ in different namespace [-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++ 不发出警告)。

最佳答案

假设您在 C++11 模式下编译它(因为 clang 没有发出警告)并且这个特化在全局命名空间中,那么您的代码没有任何问题。这是一个 g++ 错误。 §14.7.3 [temp.expl.spec]/p2:

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 block 并将特化放在那里。

参见 CWG issue 374GCC bug 56480 .

关于c++ - 警告 : specialization of template in different namespace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25594644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com