簡體   English   中英

C ++:模板化代碼可使用clang ++編譯並正常運行,但對於g ++則失敗

[英]C++: Templated code compiles and runs fine with clang++, but fails with g++

看一下鏈表的此實現:

#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;
};

給我帶來問題的部分是該列表的iterator實現。 迭代器模板應該提供可變和const迭代器。

這是一個使用此實現的程序:

#include "List.h"
#include <iostream>

int main( int argc, const char *argv[] ) {
    List<int> l;

    std::cout << l.len() << std::endl;
    return 0;
}

如果我使用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      

此錯誤的原因是什么? 我怎樣才能解決這個問題?

問題似乎在這里:

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;
    }

這就是說:對於isconst所有值(外部模板參數),請定義模板函數template<bool A> bool operator==

因此,實例化iterator<true>將定義template<bool A> bool operator== ,然后實例化iterator<false>將再次定義template<bool A> bool operator== ,從而導致重新定義錯誤。

解決方案:卸下內部模板。 iterator每個實例僅定義其自己的operator==

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;
    }

(這里的iterator自動引用iterator<isconst> ,即當前實例。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM