Determining if an std::string contains a numerical type I recently came across this SO question asking for methods to determine if a std::string represents an integer. There is also this SO question for floats. I decided to code up a few methods consolidating these questions.

#include &lt;string&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;cctype&gt;

namespace detail
{
    const auto is_digit = [] (const char c) { return std::isdigit(c); };
}

bool is_integral(const std::string&amp; str)
{
    return !str.empty() &amp;&amp; std::all_of(std::cbegin(str), std::cend(str), detail::is_digit);
}

bool is_floating_point(const std::string&amp; str)
{
    using std::cbegin; using std::cend;

    auto it = std::find_if_not(cbegin(str), cend(str), detail::is_digit);

    return it != cend(str) &amp;&amp; *it++ == '.' &amp;&amp; std::all_of(it, cend(str), detail::is_digit);
}

bool is_arithmetic(const std::string&amp; str)
{
    using std::cbegin; using std::cend;

    if (str.empty()) return false;

    auto it = std::find_if_not(cbegin(str), cend(str), detail::is_digit);

    return it == cend(str) || (*it++ == '.' &amp;&amp; std::all_of(it, cend(str), detail::is_digit));
}


Here's an example/expected behaviour:

#include &lt;iostream&gt;

int main()
{
    std::string empty {};
    std::string integer {"123"};
    std::string floating1 {"123."};
    std::string floating2 {"123.4"};
    std::string neither1 {"123..4"};
    std::string neither2 {"1r23"};

    std::cout &lt;&lt; std::boolalpha;

    std::cout &lt;&lt; is_integral(empty) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_floating_point(empty) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_arithmetic(empty) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; std::endl;

    std::cout &lt;&lt; is_integral(integer) &lt;&lt; std::endl; // true
    std::cout &lt;&lt; is_floating_point(integer) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_arithmetic(integer) &lt;&lt; std::endl; // true
    std::cout &lt;&lt; std::endl;

    std::cout &lt;&lt; is_integral(floating1) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_floating_point(floating1) &lt;&lt; std::endl; // true
    std::cout &lt;&lt; is_arithmetic(floating1) &lt;&lt; std::endl; // true
    std::cout &lt;&lt; std::endl;

    std::cout &lt;&lt; is_integral(floating2) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_floating_point(floating2) &lt;&lt; std::endl; // true
    std::cout &lt;&lt; is_arithmetic(floating2) &lt;&lt; std::endl; // true
    std::cout &lt;&lt; std::endl;

    std::cout &lt;&lt; is_integral(neither1) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_floating_point(neither1) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_arithmetic(neither1) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; std::endl;

    std::cout &lt;&lt; is_integral(neither2) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_floating_point(neither2) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_arithmetic(neither2) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; std::endl;

    std::cout &lt;&lt; is_integral(neither2) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_floating_point(neither2) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; is_arithmetic(neither2) &lt;&lt; std::endl; // false
    std::cout &lt;&lt; std::endl;

    return 0;
}


I'm looking for any improvements.
Functional issues

Your code doesn't consider negative numbers to be integral. You're also missing support for things like 1e4 for floating point. Ultimately, std::stoi and std::stof might be better for your needs:

boost::optional&lt;int&gt; convert_integer(std::string const&amp; str) {
    try {
        size_t pos;
        int result = std::stoi(str, &amp;pos);
        if (pos == str.size()) {
            return result;
        }
        else {
            return boost::none;
        }
    }
    catch (std::invalid_argument&amp; ) {
        return boost::none;
    }
    catch (std::out_of_range&amp; ) {
        return boost::none;
    }
} 


and similar for floating point.

is_digit

There's no reason for this to be a lambda. It's actually quite a bit shorter if you just write it as a function:

const auto is_digit = [] (const char c) { return std::isdigit(c); };
bool is_digit(char c) { return std::isdigit(c); }


using std::cbegin;

There's actually no reason to bring this in, since you could already make an unqualified call to cbegin() thanks to ADL. Also, you don't even need cbegin() since begin() would have done the same thing (str is a reference to const in all of your functions).

Too much action

This is really busy:

return it != cend(str) &amp;&amp; *it++ == '.' &amp;&amp; std::all_of(it, cend(str), detail::is_digit);


And it's confusing since you increment it in the middle. Which happens to work, but I had to stop and really think about it to convince myself that it does. You could simply shift the ++ into the all_of:

return it != cend(str) &amp;&amp; 
    *it == '.' &amp;&amp; 
    std::all_of(it + 1, cend(str), detail::is_digit);


Since there's no modification going on here, each of the 3 parts are easier to reason about. 

Just std::string?

A logical extension would be to change your functions to take iterator pairs, or const char* pairs. You could provide convenience string methods to forward to the helpers, but it'd be potentially more useful to have

// one of...
bool is_integral(const char* first, const char* last);
template &lt;class It&gt;
bool is_integral(It first, It last);

// ... and a helper to forward
bool is_integral(const std::string&amp; str);