I'm using Visual Studio 2012 with C++11. I have a simple object that has a constructor that takes an int like the following:
struct A
{
A(int val);
};
I then have a function that has two implementations; one that takes a reference to an A
object and another that takes a pointer to some other object (I'll just use string
for simplicity):
void foo(const A & val);
void foo(const string * val);
If I call the function with a hard-coded integer value then it always ends up calling the pointer implementation like so:
foo(0); // Will convert 0 to a const string *
Is there any way to make sure that it converts it to A
instead of a pointer? This is causing really hard to find bugs that I just now realized is a major problem. The workaround is to just construct the A
in place like this:
foo(A(0));
I'd much rather have it prefer converting to A
, but if that's not possible then it should somehow detect when this error might be happening and output a compiler error or warning or something.
EDIT: This specifically happens when passing 0 only. I assume this is because of the C way of detecting NULL. Is there some way to make this not do that? Or work around it somehow?
Aucun commentaire:
Enregistrer un commentaire