after instantiation"(CLang)-6ren"> after instantiation"(CLang)-我有一些代码可以在 MSVC 下正常编译(或者说发送给我的 Windows 开发人员),但在 CLang 下会出错。环顾四周,我发现 CLang 在解析模板特化方面确实更加严格,但我不确定在我的案例中-6ren">
gpt4 book ai didi

c++ - "Explicit specialization of std::iterator_traits after instantiation"(CLang)

转载 作者:太空狗 更新时间:2023-10-29 23:53:03 29 4
gpt4 key购买 nike

我有一些代码可以在 MSVC 下正常编译(或者说发送给我的 Windows 开发人员),但在 CLang 下会出错。环顾四周,我发现 CLang 在解析模板特化方面确实更加严格,但我不确定在我的案例中我应该把特化放在哪里。基本上我的一个文件有这样的结构:

template<>
struct iterator_traits< char * > // error is here
{
typedef random_access_iterator_tag iterator_category;
typedef char value_type;
typedef ptrdiff_t difference_type;
typedef difference_type distance_type;
typedef char * pointer;
typedef char & reference;
};

这是在 namespace std block 中。错误信息是:

Explicit specialization of 'std::iterator_traits<char *>' after instantiation

同一错误消息的另一部分(通过在 Xcode 中“展开”错误消息查看)说 Implicit instantiation first required here,单击它会转到 STL_iterator.h,特别是这一行(第 642 行):

typedef typename iterator_traits<_Iterator>::iterator_category
iterator_category;

有谁知道在这种情况下正确的做法是什么?我见过涉及类的示例,但从未见过涉及结构的示例。

最佳答案

编译器提示您在实例化通用模板后尝试特化模板——到那个时候,编译器已经使用了通用模板进行实例化,它不能返回并使用你的特化。换句话说,像这样:

template <typename T>
struct X
{
// Generic implementation
};

// Instantiate template by using it in any way
X<int> foo;

template<>
struct X<int>
{
// Specialization implementation for int
};

修复方法是在实例化之前定义特化,因此在本例中,您将移动 X<int>特化到哪里X<int>被使用。

请注意,STL 已经定义了 std::iterator_trait 的特化对于指针类型,因此无需在此处为 char* 定义您自己的特化.您通常只会对非指针的用户定义的迭代器类型执行此操作。参见 C++03 标准的 §24.3.1/2:

[The template iterator_traits<Iterator>] is specialized for pointers as

template<class T> struct iterator_traits<T*> {
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef random_access_iterator_tag iterator_category;
};

and for pointers to const as

template<class T> struct iterator_traits<const T*> {
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
typedef random_access_iterator_tag iterator_category;
};

所以没有必要提供你自己的 std::iterator_traits<char*>特化。自 char*不是用户定义的类型,根据标准它也是未定义的行为。 §17.4.3.1/1 说:

It is undefined for a C + + program to add declarations or definitions to namespace std or namespaces within namespace std unless otherwise specified. A program may add template specializations for any standard library template to namespace std. Such a specialization (complete or partial) of a standard library template results in undefined behavior unless the declaration depends on a user-defined name of external linkage and unless the specialization meets the standard library requirements for the original template.163)

163) Any library code that instantiates other library templates must be prepared to work adequately with any user-supplied specialization that meets the minimum requirements of the Standard

关于c++ - "Explicit specialization of std::iterator_traits<char *> after instantiation"(CLang),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12732042/

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