Stopping the implicit conversion function

Today I came across a strange situation when I needed a function to implicitly convert values.

After browsing google, I found http://www.devx.com/cplus/10MinuteSolution/37078/1954

But I thought it would be a little silly to use the overload function for every other type that I want to block, so I did it.


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 that I added const to int, so the variable is not editable, however, when I started to compile, but should not, 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?

+5
source share
2

:

template <class T>
void foo(const T& t);

template <>
void foo<int>(const int& t)
{

}

int main(){
  foo(9); // will compile
  foo(9.0); // will not compile
  return 0;
}

, int, , - , .

+14

const, const.

A char int, , , const int&, .

+7

All Articles