diff cpp/to_int/to_int.hpp @ 642:18aa7181e0c3

to_int: use std::optional instead of boost
author David Demelier <markand@malikania.fr>
date Wed, 01 Aug 2018 13:56:54 +0200
parents 5dd3347df00d
children 2968cc4edd4c
line wrap: on
line diff
--- a/cpp/to_int/to_int.hpp	Fri May 04 17:32:00 2018 +0200
+++ b/cpp/to_int/to_int.hpp	Wed Aug 01 13:56:54 2018 +0200
@@ -24,10 +24,9 @@
  * \brief Safely convert string to integers.
  */
 
-#include <boost/optional.hpp>
-
 #include <cstdlib>
 #include <limits>
+#include <optional>
 #include <string>
 #include <type_traits>
 
@@ -37,10 +36,10 @@
  * \param str the string to convert
  * \param min the minimum value allowed
  * \param max the maximum value allowed
- * \return the value or boost::none if not convertible
+ * \return the value or std::none if not convertible
  */
 template <typename T = int>
-boost::optional<T> to_int(const std::string& str,
+std::optional<T> to_int(const std::string& str,
                           T min = std::numeric_limits<T>::min(),
                           T max = std::numeric_limits<T>::max()) noexcept
 {
@@ -50,7 +49,7 @@
     auto v = std::strtoll(str.c_str(), &end, 10);
 
     if (*end != '\0' || v < min || v > max)
-        return boost::none;
+        return std::nullopt;
 
     return static_cast<T>(v);
 }
@@ -62,10 +61,10 @@
  * \param str the string to convert
  * \param min the minimum value allowed
  * \param max the maximum value allowed
- * \return the value or boost::none if not convertible
+ * \return the value or std::none if not convertible
  */
 template <typename T = unsigned>
-boost::optional<T> to_uint(const std::string& str,
+std::optional<T> to_uint(const std::string& str,
                            T min = std::numeric_limits<T>::min(),
                            T max = std::numeric_limits<T>::max()) noexcept
 {
@@ -75,7 +74,7 @@
     auto v = std::strtoull(str.c_str(), &end, 10);
 
     if (*end != '\0' || v < min || v > max)
-        return boost::none;
+        return std::nullopt;
 
     return static_cast<T>(v);
 }