1

I have written a template function which takes int32, bool and int16 as an input. However for bool, I am getting this warning. Any ideas, how can I resolve it.

template<class T>
void GameControl::assignValues(char *gaff, T &output)
{
   output = T(atoi(gaff));
}

The function calls are as follows:

int32 intout;
assignValues("1234", intout);
bool boolout;
assignValues("1234", boolout);

can anyone tell, how to get rid of the warning?

EDIT: This worked, but not sure of the consequences. I just suppressed the warning.

#pragma warning( push )
#pragma warning( disable : 4101)
// Your function
#pragma warning( pop )
Yash Kapoor
  • 93
  • 2
  • 12
  • 1
    Perhaps *specialize* the function for `bool`? – Some programmer dude Feb 23 '18 at 08:34
  • On an unrelated note, the `atoi` function is usually discouraged, as it's impossible to differ between a valid input `"0"` and an invalid input which results in `0` being returned. I recommend [`std::strtol`](http://en.cppreference.com/w/cpp/string/byte/strtol) or [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) instead. – Some programmer dude Feb 23 '18 at 08:36
  • 4
    Why isn't this returning a value, instead of using an output "parameter"? Your code could just read `int23 intout = assignValues("1234");` – juanchopanza Feb 23 '18 at 08:36
  • Building on the comment by @juanchopanza, for `bool` it would be `bool boolout = assignValues("1234") != 0;` – Some programmer dude Feb 23 '18 at 08:50
  • This is just a dummy function I have created for the issue, the original function is already returning a value and is different, but only this functionality is causing the issue. I just want to avoid code duplication – Yash Kapoor Feb 23 '18 at 09:22

2 Answers2

3

Your first job is to change the function parameter list to a const char* gaff since standard C++ does not allow the decay of a const char[N] to a char*. Ironic it is that your compiler doesn't issue a diagnostic but complains about the dubious cast!

As for that warning, you can force the issue with

output = static_cast<T>(atoi(gaff));

Most compilers then will assume that you know what you're doing. If you still get a warning, then specialise the template function for the bool case; a solution which, on balance, I think I prefer (other than the pragmatic approach of switching off the warning for that particular function):

template<>
void assignValues(const char *gaff, bool &output)
{
   output = atoi(gaff) != 0;
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

You can fully specialize the function template for bool:

template<>
void GameControl::assignValues<bool>(char *gaff, boot &output)
{
   output = !!atoi(gaff);
}
JFMR
  • 23,265
  • 4
  • 52
  • 76
  • 1
    This does solve the issue, but now there is a duplication of code, I wanted to avoid that. Is it possible? – Yash Kapoor Feb 23 '18 at 09:20
  • What do you mean by *duplication of code* ? This case is **only** for `T = bool`. For all other values of `T` (e.g.: `int`), your *primary template* is used instead. – JFMR Feb 23 '18 at 09:23
  • Just in case I modify the functionality of the code in the template function, then I would have copy paste it in the bool module as well. That is what I want to avoid. – Yash Kapoor Feb 23 '18 at 09:26
  • Well, if the other approaches based on casting did still show the warning, I would suggest to individually [suppress that warning](https://stackoverflow.com/questions/7159348/disable-single-warning-error) (`C4800`). – JFMR Feb 23 '18 at 09:34
  • 1
    Thanks, I guess that is a better solution. I'll just suppress warning, If it doesn't perform as expected, then I will write a separate function. – Yash Kapoor Feb 23 '18 at 09:37