I want to restrict automatic type conversation in C++ function calls.
I have the below piece of code in which a function is called which takes an int value, but while calling the same a float value is passed.
I want to restrict this.
Now it is showing a compile time warning message.
But I need an error message instead of the warning one.
#include <iostream>
usingnamespace std;
class A
{
public:
void display(int value)
{
cout<<"Passed Value :: "<<value<<endl;
}
};
int main()
{
float floatValue = 5.5f;
A a;
/// Below statement should give an error as float value
/// is being passed instead of integer value.
a.display(floatValue);
return 0;
}
Declare a private overload for the types you don't want:
1 2 3 4 5 6 7 8 9
class A
{
void display ( double value ); // discards double and floatpublic:
void display(int value)
{
cout<<"Passed Value :: "<<value<<endl;
}
};
1 2 3 4 5 6 7 8 9 10
class A
{
template < class T >
void display ( T value ); // with this, the compiler should accept only intpublic:
void display(int value)
{
cout<<"Passed Value :: "<<value<<endl;
}
};