labs-edaf30/lab4/string_cast.h

15 lines
384 B
C
Raw Permalink Normal View History

2024-12-11 15:54:56 +01:00
#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;
}