17 Apr, 2007, kiasyn wrote in the 1st comment:
Votes: 0
is there any reason not to use something like this?

Usage:
unsigned int one = strToUNumber<unsigned int>( "1" );
double two = strToNumber<double>("2");
unsigned long long three = strToUNumber<unsigned long long>( "3" );


template<class T>
T strToNumber( const string &str )
{
T num = 0;
bool neg = false;
for ( string::size_type i = 0; i < str.length(); i++ )
{
if ( isspace(str[i]) || !isdigit(str[i]) )
continue;
if ( str[i] == '-' )
neg = true;
num = num * 10 + str[i]-'0';
}
if ( neg )
num = -num;
return num;
}
/* Unsigned Number */
template<class T>
T strToUNumber( const string &str )
{
T num = 0;
bool neg = false;
for ( string::size_type i = 0; i < str.length(); i++ )
{
if ( isspace(str[i]) || !isdigit(str[i]) )
continue;
if ( str[i] == '-' )
throw exception( "`-' sign used in unsigned number" );
num = num * 10 + str[i]-'0';
}
return num;
}
18 Apr, 2007, Dorian wrote in the 2nd comment:
Votes: 0
Actually, you make it more complicated than it has to be:

#include <sstream>
#include <exception>

template<typename Dest, typename Source> Dest convert(const Source &s)
{
std::stringstream ss;
ss << s;
Dest ret;
if( !(ss >> ret) ) throw std::exception("Failed to convert!");
return ret;
}


This template will convert any type to any other type as long as the following conditions are met:

1. Source has an operator<< defined
2. Dest has an operator>> defined
3. Dest has a public constructor and copy constructor

So:

int i = converter<int>("4");
std::string s = converter<std::string>(3453);
unsigned int x = converter<unsigned int>(s);


These will all work.

I would also suggest Googling "Boost" and look up lexical_cast.
18 Apr, 2007, kiasyn wrote in the 3rd comment:
Votes: 0
Pretty clever, thanks for the advice. :D
19 Apr, 2007, Guest wrote in the 4th comment:
Votes: 0
Maybe it's just my lack of C++ knowledge, but why worry about replacing atoi() with something else that does the same basic thing?
19 Apr, 2007, Pedlar wrote in the 5th comment:
Votes: 0
can add more functionality to it?
19 Apr, 2007, Dorian wrote in the 6th comment:
Votes: 0
Samson said:
Maybe it's just my lack of C++ knowledge, but why worry about replacing atoi() with something else that does the same basic thing?


Actually, it does quite more. You can convert integers of any type (16-bit,32-bit,64-bit
  • ,etc.) to a string. You can convert a string to an integer of any type. You can convert a string to a floating point number or vice versa. The fun doesn't even end there since you can convert any type to any other type (like you could convert class X to class Y) as long as the three conditions I laid out above are satisfied.

  • If anything else, this is type-safe and handles errors far more gracefully.

  • Since 64-bit types are relatively new, you need to make sure your implementation's iostream library can handle them before they will work. All the other built-in types should work easily.
  • 25 Apr, 2007, kiasyn wrote in the 7th comment:
    Votes: 0
    Dorian said:
    Actually, you make it more complicated than it has to be:

    #include <sstream>
    #include <exception>

    template<typename Dest, typename Source> Dest convert(const Source &s)
    {
    std::stringstream ss;
    ss << s;
    Dest ret;
    if( !(ss >> ret) ) throw std::exception("Failed to convert!");
    return ret;
    }


    This template will convert any type to any other type as long as the following conditions are met:

    1. Source has an operator<< defined
    2. Dest has an operator>> defined
    3. Dest has a public constructor and copy constructor

    So:

    int i = converter<int>("4");
    std::string s = converter<std::string>(3453);
    unsigned int x = converter<unsigned int>(s);


    These will all work.

    I would also suggest Googling "Boost" and look up lexical_cast.


    if( !(ss >> ret) ) throw std::exception("Failed to convert!");


    im actually wondering how needed that bit is, wouldnt it just fail to compile if there was no operator for it?
    25 Apr, 2007, Dorian wrote in the 8th comment:
    Votes: 0
    kiasyn said:
    if( !(ss >> ret) ) throw std::exception("Failed to convert!");


    im actually wondering how needed that bit is, wouldnt it just fail to compile if there was no operator for it?


    The exception is to tell the user that the runtime conversation failed. This is separate from the compilation error that there was no appropriate operator>> available.
    0.0/8