+8  A: 

Looks good to me, but you can use isalnum(c) instead of isalpha and isdigit.

jopa
+1. Also you can use `isspace()` if you want to count other types of whitespace (e.g. tab).
Matt Curtis
The only suggestion that I have for improvement is to not have `_not_` in function names as it can make for unreadable code. Consider this instead: `return find_if(str.begin(), str.end(), is_alnum_space) != str.end();`
Johnsyweb
@Johnsyweb: That won't work though. That will return `true` for a string that contains *at least one* alphanumeric+space character, but I want to ensure that they are *all* alphanumeric+space characters. The purpose of the `find_if` is to find the first element that satisfies the predicate, the predicate being "not alphanumeric or space". If we get `str.end()` we know that no character satisfied that predicate, so they must all be alphanumeric or space characters.
dreamlax
@dreamlax: You are right. It has clearly been a long week :-)
Johnsyweb
A: 

Minor points, but if you want is_not_alnum_space() to be a helper function that is only visible in that particular compilation unit, you should put it in an anonymous namespace instead of making it static:

namespace {
bool is_not_alnum_space(char c)
{
    return !(isalpha(c) || isdigit(c) || (c == ' '));
}
}
...etc
Ilkka
Thanks for the advice, what is the benefit of this anonymous namespaces over using static functions?
dreamlax
It's deprecated by current c++ standards for this use.
Ilkka
+1  A: 

You can also do this with binders so you can drop the helper function. I'd recommend Boost Binders as they are much easier to use then the standard library binders:

bool string_is_valid(const std::string &str)
{
    return find_if(str.begin(), str.end(),
        !boost::bind(isalnum, _1) || boost::bind(std::not_equal_to<char>, _1, ' ')) == str.end();
}
R Samuel Klatchko
Wow that is crazy and cool at the same time. Although, I don't what to check for all types of whitespace, I just want to check for the space character itself. Is that still possible with binder?
dreamlax
@dreamlax - yes, that's possible. I've updated the answer to do that.
R Samuel Klatchko
+1  A: 

And looking forward to C++0x, you'll be able to use lambda functions (you can try this out with gcc 4.5 or VS2010):

bool string_is_valid(const std::string &str)
{
    return find_if(str.begin(), str.end(), 
        [](char c) { return !(isalnum(c) || (c == ' ')); }) == str.end();
}
R Samuel Klatchko
The syntax is initially confusing, but I'm sure I'll get used to it.
dreamlax