Is there a way to by using some metaprogramming trick make situation like this works:
int* get();//this fnc returns pointer to int OR nullptr
int k = 1;
//this is the operator which is supposed to compare value and pointer
template<class T>
bool operator!=(const T& left, const T* right)
{
if (right)
{
return left != *right;
}
else
{
return false;
}
}
//And this is the code fragment which interests me most
if (k != get())
{
///
}
The crux is that I would like NOT TO change this line k != get() and yet for some reason my operator!= seems not to work. What’s the problem?
As already mentioned in other answers that you cannot have
operator !=for non userdefined types likeint,charand so on.One option is to wrap
intinside your user definedstructand achieve the goal.Now declare