Chuck Wolber
Chuck Wolber

Reputation: 519

Passing const reference pointer fails to match method signature

The following code passes a const pointer reference to a size() helper function. It only works if I remove the const or the & reference operator from the helper function.

#include <iostream>

using namespace std;                                                                   

template <typename T>
class Test {
   public:
      Test();
      int size();
      void insert(T);
   private:
      struct Node {
         T value;
         Node* left;
         Node* right;                                                              
      };
      Node* root;
      int size(const Node*& node);
};

template <typename T>
Test<T>::Test() { root = nullptr;}                                                                                  

template <typename T>
int Test<T>::size() {return size(root);}                                                                                  

template <typename T>
int Test<T>::size(const Node*& node) {
   if (node != nullptr)
      return 1 + size(node->left) + size(node->right);                             
   return 0;
}

int main() {
   Test<int> t;
   cout << "Size: " << t.size() << endl;
}

I get the following compiler errors when I compile this code as C++11:

main.cpp:31:11: error: no matching member function for call to 'size'
   return size(root);
          ^~~~
main.cpp:43:26: note: in instantiation of member function 'Test<int>::size' requested here
   cout << "Size: " << t.size() << endl;
                         ^
main.cpp:21:11: note: candidate function not viable: no known conversion from 'Test<int>::Node *' to 'const Test<int>::Node *&' for 1st argument
      int size(const Node*& node);
          ^
main.cpp:10:11: note: candidate function not viable: requires 0 arguments, but 1 was provided
      int size();
          ^
1 error generated.

However, if I simply remove the const or the reference operator (&) from the helper function that size() calls, it compiles and runs exactly as expected.

In other words, either of the following works:

int size(Node*& node);
template <typename T> int Test<T>::size(Node*& node)
int size(const Node* node);
template <typename T> int Test<T>::size(const Node* node)

But this does not:

int size(const Node*& node);
template <typename T> int Test<T>::size(const Node*& node)

The declaration and implementation seem identical in all three cases, so I am having a hard time figuring out why the case with the const reference fails.

Upvotes: 0

Views: 30

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

If it were legal to pass a pointer to non-const object where a reference to pointer to const object is expected, then it would be possible to violate const correctness. Consider:

const int c = 42;

void f(const int*& p) {
  // Make p point to c
  p = &c;
}

int* q;
f(q);  // hypothetical, doesn't compile
// Now q points to c
*q = 84;  // oops, modifying a const object

Upvotes: 1

Related Questions