Skip to content

Commit 2ff4242

Browse files
committed
Provide to_string() for std17::string_view in namespace nonstd
1 parent 6ba7b52 commit 2ff4242

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ Initially writing code for `char` type strings, followed by generalising and ena
3939
template< typename T >
4040
std::string contents(std::vector<T> const & coll)
4141
{
42-
// using to_string() for nonstd::string::string_view:
42+
// using nonstd::to_string() for nonstd::std17::string_view:
43+
using nonstd::to_string;
4344

4445
std::stringstream os;
4546
for ( auto const & elem : coll )

example/01-basic.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
template< typename T >
1212
std::string contents(std::vector<T> const & coll)
1313
{
14-
// using to_string() for nonstd::string::string_view:
14+
// using nonstd::to_string() for nonstd::std17::string_view:
15+
using nonstd::to_string;
1516

1617
std::stringstream os;
1718
for ( auto const & elem : coll )
18-
os << "'" << std::string(elem) << "', ";
19+
os << "'" << to_string(elem) << "', ";
1920
return os.str();
2021
}
2122

@@ -24,14 +25,15 @@ std::string contents(std::vector<T> const & coll)
2425
template< typename T >
2526
std::string contents(std::vector<T> const & coll)
2627
{
27-
// using to_string() for nonstd::string::string_view:
28+
// using nonstd::to_string() for nonstd::std17::string_view:
29+
using nonstd::to_string;
2830

2931
typename std::vector<T>::const_iterator pos = coll.begin();
3032
typename std::vector<T>::const_iterator end = coll.end();
3133

3234
std::stringstream os;
3335
for ( ; pos != end; ++pos )
34-
os << "'" << std::string(*pos) << "', ";
36+
os << "'" << to_string(*pos) << "', ";
3537
return os.str();
3638
}
3739

@@ -53,4 +55,4 @@ int main()
5355
// g++ -std=c++11 -Wall -I../include -o 01-basic.exe 01-basic.cpp && 01-basic.exe
5456

5557
// Output:
56-
// ['Hello', ' world', ]
58+
// ['Hello', 'world', ]

include/nonstd/string.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,10 @@ split_right( std17::basic_string_view<CharT> , std17::basic_string_view<CharT>
16631663
// Define requested functions:
16641664
//
16651665

1666+
// Provide to_string() for std17::string_view:
1667+
1668+
using detail::to_string;
1669+
16661670
#if string_CONFIG_PROVIDE_CHAR_T
16671671

16681672
string_MK_IS_EMPTY ( char )

0 commit comments

Comments
 (0)