0
votes
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>

using namespace std;

bool notSpace(char c) {
    return !isspace(c);
}
bool isSpace(char c) {
    return isspace(c);
}
vector<string> split(const string& s) {
    vector<string> words;
    string::const_iterator i = s.begin();
    while (i != s.end()) {
        i = find_if(i, s.end(), notSpace); // "              "
        if (i != s.end()) {
            string::const_iterator j = i;
            j = find_if(i, s.end(), isSpace);
            words.push_back(string(i, j));
            i = j;
        }
    }
    return words;
}
int main() {
    string test = "Hello world, I'm a simple guy";
    vector<string> words = split(test);
    for (vector<string>::size_type i = 0; i < words.size();i++) {
        cout << words[i] << endl;
    }
    return 0;
}

When I compile the code I get this warning:

warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)

on the return of this function:

bool isSpace(char c) {
    return isspace(c);
}

Is good habit changing isspace(c) to (isspace(c) != 0) ? Or is it just an unnecessary fussiness?

1
Why do you need the functions at all?user2100815
The compiler is telling you that it is basically doing what you have suggested (to convert the int return to a bool). So in my opinion you might as well add the code to shut-up the warning.Richard Critten
You'll gain a great deal more by changing the parameter to isSpace and notSpace from char to unsigned char. This way you won't get completely broken behavior when the user enters characters with accents, umlauts, etc.Jerry Coffin

1 Answers

0
votes

Take a look at the code below:

#include <iostream>
using namespace std;
bool f()
{
    return 2;
}
int main()
{
  cout <<f()<<endl;
  return 0;
}

it will print 1 when you return 2, that's why you get the warning. someone may think a bool is kind of small integer, but it isn't.

If you go back to C, there was no bool type, that's why many C methods (like isspace) returns int, even WINDOWS type of BOOL is actually kind of integer and can return other values but TRUE (1) or FALSE (0).