diff --git a/include/string_span.h b/include/string_span.h index beccee4..133712e 100644 --- a/include/string_span.h +++ b/include/string_span.h @@ -22,6 +22,16 @@ #include "span.h" #include +// VS 2013 workarounds +#ifdef _MSC_VER +#if _MSC_VER <= 1800 + +#pragma push_macro("GSL_MSVC_HAS_TYPE_DEDUCTION_BUG") +#define GSL_MSVC_HAS_TYPE_DEDUCTION_BUG + +#endif // _MSC_VER <= 1800 +#endif // _MSC_VER + namespace gsl { // @@ -34,16 +44,16 @@ namespace gsl // type system for these types that will not either incur significant runtime costs or // (sometimes needlessly) break existing programs when introduced. // -template +template using czstring = const char*; -template +template using cwzstring = const wchar_t*; -template +template using zstring = char*; -template +template using wzstring = wchar_t*; // @@ -134,12 +144,37 @@ basic_string_span::type, dy // // to_string() allow (explicit) conversions from string_span to string // -template +#ifndef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG + +template std::basic_string::type> to_string(basic_string_span view) +{ + return{ view.data(), static_cast(view.length()) }; +} + +#else + +std::string to_string(cstring_span<> view) { return{ view.data(), view.length() }; } +std::string to_string(string_span<> view) +{ + return{ view.data(), view.length() }; +} + +std::wstring to_string(cwstring_span<> view) +{ + return{ view.data(), view.length() }; +} + +std::wstring to_string(wstring_span<> view) +{ + return{ view.data(), view.length() }; +} + +#endif template class basic_zstring_builder @@ -178,4 +213,15 @@ template using wzstring_builder = basic_zstring_builder; } +// VS 2013 workarounds +#ifdef _MSC_VER +#if _MSC_VER <= 1800 + +#pragma pop_macro("GSL_MSVC_HAS_TYPE_DEDUCTION_BUG") +#undef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG + +#endif // _MSC_VER <= 1800 +#endif // _MSC_VER + + #endif // GSL_STRING_SPAN_H diff --git a/tests/string_span_tests.cpp b/tests/string_span_tests.cpp index dc3ccf5..ab48fcb 100644 --- a/tests/string_span_tests.cpp +++ b/tests/string_span_tests.cpp @@ -22,6 +22,7 @@ using namespace std; using namespace gsl; + SUITE(string_span_tests) { @@ -104,6 +105,18 @@ SUITE(string_span_tests) string_span<> v3 = "Hello"; #endif } + + TEST(TestToString) + { + auto s = gsl::to_string(cstring_span<>{}); + CHECK(s.length() == 0); + + char stack_string[] = "Hello"; + cstring_span<> v = ensure_z(stack_string); + auto s2 = gsl::to_string(v); + CHECK(s2.length() == v.length()); + CHECK(s2.length() == 5); + } } int main(int, const char *[])