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











up vote
0
down vote

favorite












Take a look at this implementation of a linked list:



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


The part giving me problems is the iterator implementation for this list. The iterator template is supposed to provide both mutable and const iterators.



This is a program which uses this implementation:



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

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

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


The program compiles and runs fine if I use clang++, but the compilation fails for g++ with the following error:



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


What's the cause of this error? How can I fix this?










share|improve this question
























  • I would try the friend statement inside the class and the body declaration of the operator= to be in global scope and declared inline.
    – Fabio
    Nov 11 at 13:53

















up vote
0
down vote

favorite












Take a look at this implementation of a linked list:



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


The part giving me problems is the iterator implementation for this list. The iterator template is supposed to provide both mutable and const iterators.



This is a program which uses this implementation:



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

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

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


The program compiles and runs fine if I use clang++, but the compilation fails for g++ with the following error:



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


What's the cause of this error? How can I fix this?










share|improve this question
























  • I would try the friend statement inside the class and the body declaration of the operator= to be in global scope and declared inline.
    – Fabio
    Nov 11 at 13:53















up vote
0
down vote

favorite









up vote
0
down vote

favorite











Take a look at this implementation of a linked list:



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


The part giving me problems is the iterator implementation for this list. The iterator template is supposed to provide both mutable and const iterators.



This is a program which uses this implementation:



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

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

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


The program compiles and runs fine if I use clang++, but the compilation fails for g++ with the following error:



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


What's the cause of this error? How can I fix this?










share|improve this question















Take a look at this implementation of a linked list:



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


The part giving me problems is the iterator implementation for this list. The iterator template is supposed to provide both mutable and const iterators.



This is a program which uses this implementation:



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

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

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


The program compiles and runs fine if I use clang++, but the compilation fails for g++ with the following error:



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


What's the cause of this error? How can I fix this?







c++ templates iterator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:57









melpomene

57.9k54489




57.9k54489










asked Nov 11 at 13:31









saga

464315




464315












  • I would try the friend statement inside the class and the body declaration of the operator= to be in global scope and declared inline.
    – Fabio
    Nov 11 at 13:53




















  • I would try the friend statement inside the class and the body declaration of the operator= to be in global scope and declared inline.
    – Fabio
    Nov 11 at 13:53


















I would try the friend statement inside the class and the body declaration of the operator= to be in global scope and declared inline.
– Fabio
Nov 11 at 13:53






I would try the friend statement inside the class and the body declaration of the operator= to be in global scope and declared inline.
– Fabio
Nov 11 at 13:53














1 Answer
1






active

oldest

votes

















up vote
3
down vote













The problem seems to be 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;
}


This is saying: For all values of isconst (the outer template parameter), define a template function template<bool A> bool operator==.



So instantiating iterator<true> will define template<bool A> bool operator==, and then instantiating iterator<false> will define template<bool A> bool operator== again, causing a redefinition error.



Solution: Remove the inner template. Have each instantiation of iterator only define its own 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;
}


(Here iterator automatically refers to iterator<isconst>, i.e. the current instantiation.)






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249235%2fc-templated-code-compiles-and-runs-fine-with-clang-but-fails-with-g%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    The problem seems to be 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;
    }


    This is saying: For all values of isconst (the outer template parameter), define a template function template<bool A> bool operator==.



    So instantiating iterator<true> will define template<bool A> bool operator==, and then instantiating iterator<false> will define template<bool A> bool operator== again, causing a redefinition error.



    Solution: Remove the inner template. Have each instantiation of iterator only define its own 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;
    }


    (Here iterator automatically refers to iterator<isconst>, i.e. the current instantiation.)






    share|improve this answer

























      up vote
      3
      down vote













      The problem seems to be 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;
      }


      This is saying: For all values of isconst (the outer template parameter), define a template function template<bool A> bool operator==.



      So instantiating iterator<true> will define template<bool A> bool operator==, and then instantiating iterator<false> will define template<bool A> bool operator== again, causing a redefinition error.



      Solution: Remove the inner template. Have each instantiation of iterator only define its own 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;
      }


      (Here iterator automatically refers to iterator<isconst>, i.e. the current instantiation.)






      share|improve this answer























        up vote
        3
        down vote










        up vote
        3
        down vote









        The problem seems to be 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;
        }


        This is saying: For all values of isconst (the outer template parameter), define a template function template<bool A> bool operator==.



        So instantiating iterator<true> will define template<bool A> bool operator==, and then instantiating iterator<false> will define template<bool A> bool operator== again, causing a redefinition error.



        Solution: Remove the inner template. Have each instantiation of iterator only define its own 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;
        }


        (Here iterator automatically refers to iterator<isconst>, i.e. the current instantiation.)






        share|improve this answer












        The problem seems to be 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;
        }


        This is saying: For all values of isconst (the outer template parameter), define a template function template<bool A> bool operator==.



        So instantiating iterator<true> will define template<bool A> bool operator==, and then instantiating iterator<false> will define template<bool A> bool operator== again, causing a redefinition error.



        Solution: Remove the inner template. Have each instantiation of iterator only define its own 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;
        }


        (Here iterator automatically refers to iterator<isconst>, i.e. the current instantiation.)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 13:54









        melpomene

        57.9k54489




        57.9k54489






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249235%2fc-templated-code-compiles-and-runs-fine-with-clang-but-fails-with-g%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values