Consider this code, compiled with g++ v.4.8.1-4 in Code::Blocks 10.05:
#ifndef ERROR_SAMPLE_H
#define ERROR_SAMPLE_H
#include <stdexcept>
#include <sstream>
#include <string>
struct my_traits_type : public std::char_traits<char>
{};
using my_string_type=std::basic_string<char, my_traits_type>;
template <typename T, typename CharT, typename Traits, typename Alloc>
T To(const std::basic_string<CharT, Traits, Alloc>& s)
{
T value;
std::basic_istringstream<CharT, Traits, Alloc> is(s);
is >> value;
if(!(is && (is >> std::ws).eof()))
throw std::invalid_argument("Function: To<>. Unallowed conversion.");
return value;
}
#endif
and this simple main() function:
#include <iostream>
#include <string>
#include "error_sample.h"
int main()
{
size_t value=0;
std::string str_1("1");
value=To<size_t>(str_1);
std::cout << value << std::endl;
my_string_type my_string("1");
value=To<size_t>(my_string);
std::cout << value << std::endl;
return 0;
}
If we now inspect into:
is >> value;
, we discover
value
equals to 2293515, in my case.
Can anybody explain what's going on here?
Thanks in advance.