Skip to content

Commit 7940c9f

Browse files
author
josema
committed
fix compilation errors with exceptions disabled
1 parent bbee890 commit 7940c9f

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

include/cpp2util.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ constexpr bool is_escaped(std::string_view s) {
557557
}
558558

559559
inline bool string_to_int(std::string const& s, int& v, int base = 10) {
560+
#ifndef CPP2_NO_EXCEPTIONS
560561
try {
561562
v = stoi(s, nullptr, base);
562563
return true;
@@ -569,6 +570,20 @@ inline bool string_to_int(std::string const& s, int& v, int base = 10) {
569570
{
570571
return false;
571572
}
573+
#else
574+
errno = 0;
575+
char* end = nullptr;
576+
577+
const long num = std::strtol(s.c_str(), &end, base);
578+
579+
if (end == s.c_str() || *end != '\0')
580+
return false; // invalid argument
581+
if (errno == ERANGE || num < std::numeric_limits<int>::min() || num > std::numeric_limits<int>::max())
582+
return false; // out of range
583+
584+
v = static_cast<int>(num);
585+
return true;
586+
#endif
572587
}
573588

574589
template<int Base = 10>
@@ -2328,9 +2343,9 @@ class range
23282343
if (include_last) {
23292344
if constexpr (std::integral<TT>) {
23302345
if (last == std::numeric_limits<TT>::max()) {
2331-
throw std::runtime_error(
2346+
impl::Throw( std::runtime_error(
23322347
"range with last == numeric_limits<T>::max() will overflow"
2333-
);
2348+
), "range with last == numeric_limits<T>::max() will overflow");
23342349
}
23352350
}
23362351
++last;

0 commit comments

Comments
 (0)