Just FYI, template functions can make arbitrary conversions fairly trivial:
Code:
#include <sstream>
#include <cctype>
#include <typeinfo>
template <typename Dst, typename Src>
bool iocpy(Dst& dst, const Src& src)
{
bool success = true;
std::stringstream io;
if(!(io << src) || !(io >> dst))
success = false;
else
{
char ch;
while(io.get(ch))
if(!std::isspace(ch))
success = false;
}
if(!success)
dst = Dst();
return success;
}
template <typename Dst, typename Src>
Dst iocpy(const Src& src)
{
Dst dst;
if(!iocpy(dst, src))
throw std::bad_cast();
return dst;
}
#include <iostream>
int main(int argc, char** argv)
{
using namespace std;
int sum = 0;
for(int a = 1; a < argc; ++a)
{
try
{
sum += iocpy<int>(argv[a]);
}
catch(bad_cast&)
{
cout << "Sorry, '" << argv[a] << "' is not an integer!" << endl;
}
}
cout << "Sum: " << sum << endl;
}