gz-cpp-util 1.3
A c++20 library containing various utilities
from_string.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../concepts.hpp"
4#include <string>
5
6#define GZ_UTIL_STRING_CONCEPTS
7
13template<gz::util::False T>
14T fromString(const std::string& s);
15
16namespace gz::util {
17 template<typename T>
18 concept ConstructibleFromStringGlobal = requires(const std::string& s) {
19 { ::fromString<T>(s) } -> std::same_as<T>;
20 };
21
25 template<typename T>
27 std::same_as<T, int> or
28 std::same_as<T, long> or
29 std::same_as<T, long long> or
30 std::same_as<T, unsigned int> or
31 std::same_as<T, unsigned long> or
32 std::same_as<T, unsigned long long> or
33 std::same_as<T, float> or
34 std::same_as<T, double> or
35 std::same_as<T, long double> or
36 std::same_as<T, bool>;
37} // namespace gz::util
38
39namespace gz {
47
50 template<util::GetTypeFromStringImplemented T>
51 T fromString(const std::string& s);
52
54 template<> inline int fromString<int>(const std::string& s) {
55 return std::stoi(s);
56 }
58 template<> inline long fromString<long>(const std::string& s) {
59 return std::stol(s);
60 }
62 template<> inline long long fromString<long long>(const std::string& s) {
63 return std::stoll(s);
64 }
66 template<> inline unsigned int fromString<unsigned int>(const std::string& s) {
67 return std::stoul(s);
68 }
70 template<> inline unsigned long fromString<unsigned long>(const std::string& s) {
71 return std::stoul(s);
72 }
74 template<> inline unsigned long long fromString<unsigned long long>(const std::string& s) {
75 return std::stoull(s);
76 }
78 template<> inline float fromString<float>(const std::string& s) {
79 return std::stof(s);
80 }
82 template<> inline double fromString<double>(const std::string& s) {
83 return std::stod(s);
84 }
86 template<> inline long double fromString<long double>(const std::string& s) {
87 return std::stold(s);
88 }
96 template<> bool fromString<bool>(const std::string& s);
97
102 template<util::ConstructibleFromStringGlobal T>
103 inline T fromString(const std::string& s) {
104 return ::fromString<T>(s);
105 }
107
108
113 template<typename T>
114 concept ConstructibleFromString = requires(const std::string& s) {
115 { fromString<T>(s) } -> std::same_as<T>;
116 };
117} // namespace gz
118
Any type where fromString(string) exists and returns T.
Definition: from_string.hpp:114
toString is implemented for these types
Definition: from_string.hpp:26
T fromString(const std::string &s)
Declaration of fromString in global namespace, so that concepts can use it.