I am trying to write a function that takes a variable of arbitrary type and sets it to an int value only if the type of the given variable can be converted to an int. My simplified code:
template <typename T>
void getInt(T& param)
{
int int_value = calculate_int_value();
if(std::is_convertible_v<int, T>){
param = static_cast<T>(int_value);
}
}
However, when I try to compile it, I get errors such as
error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(int&)'
error: invalid conversion from 'int' to 'const char*'
Which makes me think I can't just convert an int value of arbitrary type inside an if statement, which proves it convertibility. How do I achieve my goal? Did I miss something?