0

I cannot point any difference in the signature of unmatched and available overloads.

The error says No matching call to addSupport(NestedConnection<Paragraph, NestedConnection<Line, void> >*&) But the candidates are addSupport(const NestedConnection<Paragraph, NestedConnection<Line, void> >*&) (they can be converted to const implicitly)

error: no matching function for call to ‘NestedConnection<Section, NestedConnection<Paragraph, NestedConnection<Line, void> > >::addSupport(NestedConnection<Paragraph, NestedConnection<Line, void> >*&)’
note: candidates are: void Collection<T>::addSupport(const T*&) [with T = NestedConnection<Paragraph, NestedConnection<Line, void> >]

This is what I am doing

template<typename T>
class Collection{
  public:
    typedef T Type;
    typedef std::vector<T*> CollectionT;
    typedef Collection<T> self;
  private:
    CollectionT _supports;
  public:
    void addSupport(const T*& connection){_supports.push_back(connection);};
    const CollectionT& supports() const{return _supports;}
};

template<typename T, typename C=void>
class NestedConnection: public Connection<T>, Collection<C>{
  public:
    typedef T ParentT;
    typedef C ChildT;
    typedef Connection<T> ConnectionT;
    typedef Collection<C> CollectionT;
    enum{
      leaf = 0
    };
  public:
    NestedConnection(const T* l, const T* r): Connection<T>(l, r){}
};

I am invoking by doing

NestedConnection<ParentT, ChildT>* connection = new NestedConnection<ParentT, ChildT>(lhs, rhs);
//This will be unfolded till void and it starts from 
//NestedConnection<Section, NestedConnection<Paragraph, NestedConnection<Line, void>>>
connection->addSupport(con_lr);
//con_lr:ChildT*
Neel Basu
  • 12,638
  • 12
  • 82
  • 146

1 Answers1

1

A T* can be converted to a const T* implicitly but you cannot bind a T* to a const T*&. You could if the reference was a const reference, e.g. const T* const&.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 1
    Great Nice glasses you have. So changing the signature of `addSupport(const T*&)` to `addSupport(const T*)` should fix this issue ? – Neel Basu Aug 07 '12 at 05:47
  • @NeelBasu that would compile, but is it what you want? Otherwise why did you have the original signature? – juanchopanza Aug 07 '12 at 06:11
  • @NeelBasu: Looking at your code, I would have thought that you actually needed to change the signature to take just a `T*` (or a `T* const&`) as you are trying to append to a vector of `T*`, not a vector of `const T*`. – CB Bailey Aug 07 '12 at 06:20
  • Collection doesn't need to call any `non-const` methods of contained elements. So it was a design flaw to use `typedef std::vector CollectionT` So I changed it to `typedef std::vector CollectionT;` – Neel Basu Aug 07 '12 at 07:38
  • but I couldn't understand one thing. `const T &` is a reference to `const T` then `const T* & ` will be a reference to `const T*` and I am storing `const T*` in vector. So what is the problem in having `const T*&` ? *(yes it worked with `const T*` but I want to know why `const T* &` didn't)* – Neel Basu Aug 07 '12 at 07:42
  • @NeelBasu: That signature will work as far as the body of the function is concerned but you have to call it with an lvalue of type `const T*` for the reason that I state in my answer. You can't bind the result of the conversion from `T*` to `const T*` to a reference parameter unless the reference is a reference to `const`, i.e. `const T * const &`. See here for the rationale: http://stackoverflow.com/questions/1565600/how-come-a-non-const-reference-cannot-bind-to-a-temporary-object – CB Bailey Aug 07 '12 at 07:58