14 lines
384 B
C++
14 lines
384 B
C++
#include <iostream>
|
|
#include <sstream>
|
|
#include <stdexcept> // För std::invalid_argument
|
|
|
|
// Template function for string_cast
|
|
template <typename T>
|
|
T string_cast(const std::string& str) {
|
|
std::istringstream iss(str);
|
|
T value;
|
|
if (!(iss >> value) || !(iss.eof())) {
|
|
throw std::invalid_argument("Invalid conversion from string: " + str);
|
|
}
|
|
return value;
|
|
}
|