Merge pull request #169 from neilmacintosh/dev/neilmac/to-string-test

Added workaround for MSVC 2013 compiler bug.
This commit is contained in:
Neil MacIntosh 2015-11-05 09:38:23 -08:00
commit 16f121774e
2 changed files with 64 additions and 5 deletions

View File

@ -22,6 +22,16 @@
#include "span.h" #include "span.h"
#include <cstring> #include <cstring>
// 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 namespace gsl
{ {
// //
@ -34,16 +44,16 @@ namespace gsl
// type system for these types that will not either incur significant runtime costs or // type system for these types that will not either incur significant runtime costs or
// (sometimes needlessly) break existing programs when introduced. // (sometimes needlessly) break existing programs when introduced.
// //
template<std::ptrdiff_t Max = dynamic_range> template<std::ptrdiff_t Extent = dynamic_range>
using czstring = const char*; using czstring = const char*;
template<std::ptrdiff_t Max = dynamic_range> template<std::ptrdiff_t Extent = dynamic_range>
using cwzstring = const wchar_t*; using cwzstring = const wchar_t*;
template<std::ptrdiff_t Max = dynamic_range> template<std::ptrdiff_t Extent = dynamic_range>
using zstring = char*; using zstring = char*;
template<std::ptrdiff_t Max = dynamic_range> template<std::ptrdiff_t Extent = dynamic_range>
using wzstring = wchar_t*; using wzstring = wchar_t*;
// //
@ -134,12 +144,37 @@ basic_string_span<typename std::remove_pointer<typename Cont::pointer>::type, dy
// //
// to_string() allow (explicit) conversions from string_span to string // to_string() allow (explicit) conversions from string_span to string
// //
template<class CharT, size_t Extent> #ifndef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG
template<class CharT, ptrdiff_t Extent>
std::basic_string<typename std::remove_const<CharT>::type> to_string(basic_string_span<CharT, Extent> view) std::basic_string<typename std::remove_const<CharT>::type> to_string(basic_string_span<CharT, Extent> view)
{
return{ view.data(), static_cast<size_t>(view.length()) };
}
#else
std::string to_string(cstring_span<> view)
{ {
return{ view.data(), view.length() }; 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 CharT, size_t Extent = dynamic_range> template<class CharT, size_t Extent = dynamic_range>
class basic_zstring_builder class basic_zstring_builder
@ -178,4 +213,15 @@ template <size_t Max = dynamic_range>
using wzstring_builder = basic_zstring_builder<wchar_t, Max>; using wzstring_builder = basic_zstring_builder<wchar_t, Max>;
} }
// 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 #endif // GSL_STRING_SPAN_H

View File

@ -22,6 +22,7 @@
using namespace std; using namespace std;
using namespace gsl; using namespace gsl;
SUITE(string_span_tests) SUITE(string_span_tests)
{ {
@ -104,6 +105,18 @@ SUITE(string_span_tests)
string_span<> v3 = "Hello"; string_span<> v3 = "Hello";
#endif #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 *[]) int main(int, const char *[])