gz-cpp-util 1.3
A c++20 library containing various utilities
from_string.hpp File Reference

Contains functions to construct types from string. More...

#include "../concepts.hpp"
#include <string>

Go to the source code of this file.

Concepts

concept  gz::util::GetTypeFromStringImplemented
 toString is implemented for these types
 
concept  gz::ConstructibleFromString
 Any type where fromString(string) exists and returns T.
 

Functions

template<gz::util::False T>
fromString (const std::string &s)
 Declaration of fromString in global namespace, so that concepts can use it. More...
 
Construct a type from a string
Note
The fromString() functions (except for the bool one) simply return std::stoXX. These can throw std::invalid_argument and std::out_of_range. See https://en.cppreference.com/w/cpp/string/basic_string/stol
template<util::GetTypeFromStringImplemented T>
gz::fromString (const std::string &s)
 Declaration of fromString, but only for the types for which it is implemented. More...
 
template<>
int gz::fromString< int > (const std::string &s)
 
template<>
long gz::fromString< long > (const std::string &s)
 
template<>
long long gz::fromString< long long > (const std::string &s)
 
template<>
unsigned int gz::fromString< unsigned int > (const std::string &s)
 
template<>
unsigned long gz::fromString< unsigned long > (const std::string &s)
 
template<>
unsigned long long gz::fromString< unsigned long long > (const std::string &s)
 
template<>
float gz::fromString< float > (const std::string &s)
 
template<>
double gz::fromString< double > (const std::string &s)
 
template<>
long double gz::fromString< long double > (const std::string &s)
 
template<>
bool gz::fromString< bool > (const std::string &s)
 Convert a string to bool. More...
 

Detailed Description

Contains functions to construct types from string.

Todo:
Make these functions usable with std::string_view

Function Documentation

◆ fromString() [1/2]

template<gz::util::False T>
T fromString ( const std::string &  s)

Declaration of fromString in global namespace, so that concepts can use it.

This declaration only exists so that fromString can be used in concepts.

◆ fromString() [2/2]

template<util::GetTypeFromStringImplemented T>
T gz::fromString ( const std::string &  s)
inline

Declaration of fromString, but only for the types for which it is implemented.

Construct T from a string using a fromString that is declared in global namespace.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ fromString< bool >()

template<>
bool gz::fromString< bool > ( const std::string &  s)

Convert a string to bool.

  • returns true if s = "true" or "True" or "1"
  • returns false if s = "false" or "False" or "0"
  • throws InvalidArgument otherwise

◆ fromString< double >()

template<>
double gz::fromString< double > ( const std::string &  s)
inline
Returns
std::stod(s)

◆ fromString< float >()

template<>
float gz::fromString< float > ( const std::string &  s)
inline
Returns
std::stof(s)

◆ fromString< int >()

template<>
int gz::fromString< int > ( const std::string &  s)
inline
Returns
std::stoi(s)

◆ fromString< long >()

template<>
long gz::fromString< long > ( const std::string &  s)
inline
Returns
std::stol(s)

◆ fromString< long double >()

template<>
long double gz::fromString< long double > ( const std::string &  s)
inline
Returns
std::stold(s)

◆ fromString< long long >()

template<>
long long gz::fromString< long long > ( const std::string &  s)
inline
Returns
std::stoll(s)

◆ fromString< unsigned int >()

template<>
unsigned int gz::fromString< unsigned int > ( const std::string &  s)
inline
Returns
std::stoul(s)

◆ fromString< unsigned long >()

template<>
unsigned long gz::fromString< unsigned long > ( const std::string &  s)
inline
Returns
std::stoul(s)

◆ fromString< unsigned long long >()

template<>
unsigned long long gz::fromString< unsigned long long > ( const std::string &  s)
inline
Returns
std::stoull(s)