Invalid overload resolution with const as 0

I have a class Bwith two overloaded functions int Set(B *);and int Set(const A&);. The class Aexpects a constructor argument unsigned char. When Setcalled with const unsigned chara value like 0, it is allowed Set(B*), whereas when the passed value is non-zero, it is allowed Set(const A&)(as I expected).

Overload resolution works with non-const unsigned char, but with an error const unsigned charwith a value set to 0. Why?

The following code illustrates a mismatch when Setcalled with a constant and not a constantunsigned char

#include <iostream>

using namespace std;


class A{
  char m_byteValue;
public:
  A(unsigned char c) {
    m_byteValue = c;
  }
};


class B{
  int m_a;
public:
  B(){
    m_a = 2;
  }
  int Set(B *);
  int Set(const A&);
};

int B::Set(const A& v) {
  cout << "I am in the const ref function\n";
  return 0;
}

int B::Set(B* p) {
  cout << "I am in the pointer function\n";
  return 0;
}

int main(){
  const unsigned char a = 0;
  const unsigned char b = 1;
  unsigned char c = 0;
  unsigned char d = 1;
  B var;
  var.Set(a);
  var.Set(b);
  var.Set(c);
  var.Set(d);
  return 0;
}

Conclusion (compiled using gcc 4.9.2C ++ 98): Sample - ideone on C ++ 5.1

I am in the pointer function // Why?
I am in the const ref function
I am in the const ref function
I am in the const ref function
+4
2

:

++ 98 [conv.ptr]

rvalue , .

++ 11 [conv.ptr]

0 std::nullptr_t.

const unsigned char a = 0; ++ 98. , a rvalue, , lvalue-to-rale - , unsigned char a.

a , , ++ 11.

+4

Clang , , , :

overloading_incorrect.cxx:41:13: warning: expression which evaluates
to zero treated as a null pointer constant of type 'B *'
[-Wnon-literal-null-conversion] 
var.Set(a);
        ^

NathanOliver , , ++ 11 ( -std=c++11 ).

+2

All Articles