I came across a strange situation today where I needed a function to not implicitly convert values.
After some looking on google I found this http://www.devx.com/cplus/10MinuteSolution/37078/1954
But I thought it was a bit stupid to use a function overload for every other type I want to block so instead I did this.
void function(int& ints_only_please){}
int main()
{
char a=0;
int b=0;
function(a);
function(b);
}
I showed the code to a friend and he suggested I added const before int so the variable isn't editable, however when I did started compiling fine but it shouldn't, look below to see what I mean
void function(const int& ints_only_please){}
int main()
{
char a=0;
int b=0;
function(a); //Compiler should stop here but it doesn't with const int
function(b);
}
Does anyone know why this is?
char
is implicitly convertible toint
– Marnamarne