changeset 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 3d8fae8e4447
children 29e6ef5cd606
files CMakeLists.txt cpp/to_int/to_int.hpp
diffstat 2 files changed, 8 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Fri May 04 17:32:00 2018 +0200
+++ b/CMakeLists.txt	Wed Aug 01 13:56:54 2018 +0200
@@ -19,7 +19,7 @@
 cmake_minimum_required(VERSION 3.5)
 project(code)
 
-set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED On)
 
 if (CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
--- 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);
 }