C++ C++;:模板化代码编译并运行良好,与clang++;,但在g++;
看看这个链表的实现:C++ C++;:模板化代码编译并运行良好,与clang++;,但在g++;,c++,templates,iterator,C++,Templates,Iterator,看看这个链表的实现: #include <memory> #include <type_traits> #include <iostream> using namespace std; template<typename D> class List { struct Node { shared_ptr<D> data; Node* next; Node(shared_ptr&l
#include <memory>
#include <type_traits>
#include <iostream>
using namespace std;
template<typename D>
class List {
struct Node {
shared_ptr<D> data;
Node* next;
Node(shared_ptr<D> d, Node* p, Node* n) : data(d), next(n) {}
~Node() {
data.reset();
delete next;
}
};
template <bool isconst = false>
struct iterator : public std::iterator<std::forward_iterator_tag, shared_ptr<D>> {
typedef std::forward_iterator_tag iterator_category;
typedef shared_ptr<D> value_type;
typedef std::ptrdiff_t Distance;
typedef typename conditional<isconst, const value_type&, value_type&>::type
Reference;
typedef typename conditional<isconst, const value_type*, value_type*>::type
Pointer;
typedef typename conditional<isconst, const Node*, Node*>::type
nodeptr;
iterator(nodeptr x = nullptr) : curr_node(x) {}
iterator(const iterator<false>& i) : curr_node(i.curr_node) {}
Reference operator*() const { return curr_node->data; }
Pointer operator->() const { return &(curr_node->data); }
template<bool A>
friend bool operator==(const iterator<A>& a, const iterator<A>& b) {
return a.curr_node == b.curr_node;
}
template<bool A>
friend bool operator!=(const iterator<A>& a, const iterator<A>& b) {
return !(a.curr_node == b.curr_node);
}
friend class List<D>;
iterator& operator++() {
curr_node = curr_node->next;
return *this;
}
private:
nodeptr curr_node;
};
public:
List() {
head = nullptr;
}
int len() const {
int ret = 0;
for (const auto& n : *this) {
ret++;
}
return ret;
}
~List() {
delete head;
}
std::ostream& dump(std::ostream &strm) const {
for (const auto s : *this) {
strm << *s << std::endl;
}
return strm;
}
iterator<false> begin() {
return iterator<false>(head);
}
iterator<false> end() {
return iterator<false>(nullptr);
}
iterator<true> begin() const {
return iterator<true>(head);
}
iterator<true> end() const {
return iterator<true>(nullptr);
}
private:
Node* head;
};
如果我使用clang++
,程序编译和运行正常,但是g++
的编译失败,出现以下错误:
In file included from t.cpp:1:
List.h: In instantiation of ‘struct List<int>::iterator<false>’:
List.h:136:5: required from ‘int List<D>::len() const [with D = int]’
t.cpp:7:24: required from here
List.h:64:21: error: redefinition of ‘template<bool A> bool operator==(const List<int>::iterator<isconst>&, const List<int>::iterator<isconst>&)’
friend bool operator==(const iterator<A>& a, const iterator<A>& b) {
^~~~~~~~
List.h:64:21: note: ‘template<bool A> bool operator==(const List<int>::iterator<isconst>&, const List<int>::iterator<isconst>&)’ previously declared here
List.h:69:21: error: redefinition of ‘template<bool A> bool operator!=(const List<int>::iterator<isconst>&, const List<int>::iterator<isconst>&)’
friend bool operator!=(const iterator<A>& a, const iterator<A>& b) {
^~~~~~~~
List.h:69:21: note: ‘template<bool A> bool operator!=(const List<int>::iterator<isconst>&, const List<int>::iterator<isconst>&)’ previously declared here
t.cpp:1中包含的文件中的:
h:在“struct List::iterator”的实例化中:
List.h:136:5:必须来自“int List::len()const[with D=int]”
t、 cpp:7:24:此处为必填项
List.h:64:21:错误:重新定义“模板布尔运算符==(常量列表::迭代器&,常量列表::迭代器&)”
friend bool运算符==(常量迭代器&a、常量迭代器&b){
^~~~~~~~
List.h:64:21:注意:'template bool operator==(const List::iterator&,const List::iterator&)'
List.h:69:21:错误:重新定义“模板布尔运算符!=(常量列表::迭代器&,常量列表::迭代器&)”
友元布尔运算符!=(常量迭代器&a,常量迭代器&b){
^~~~~~~~
List.h:69:21:注意:'template bool operator!=(const List::iterator&,const List::iterator&)'
导致此错误的原因是什么?如何解决此问题?问题似乎在这里:
template <bool isconst = false>
struct iterator : public std::iterator<std::forward_iterator_tag, shared_ptr<D>> {
template<bool A>
friend bool operator==(const iterator<A>& a, const iterator<A>& b) {
return a.curr_node == b.curr_node;
}
(此处
迭代器
自动引用迭代器
,即当前的实例化。)问题似乎在此处:
template <bool isconst = false>
struct iterator : public std::iterator<std::forward_iterator_tag, shared_ptr<D>> {
template<bool A>
friend bool operator==(const iterator<A>& a, const iterator<A>& b) {
return a.curr_node == b.curr_node;
}
(此处,
iterator
自动引用iterator
,即当前实例化。)我将尝试类内的friend
语句和操作符=
的主体声明,使其处于全局范围并内联声明。我将尝试类内的friend
语句和操作符=
的主体声明处于全局范围并内联声明。
template <bool isconst = false>
struct iterator : public std::iterator<std::forward_iterator_tag, shared_ptr<D>> {
friend bool operator==(const iterator& a, const iterator& b) {
return a.curr_node == b.curr_node;
}