Skip to content

Commit b01e5ac

Browse files
committed
WIP: Consider C++17 string_view
1 parent 265f6a0 commit b01e5ac

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

include/cxx.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@
77
#include <type_traits>
88
#include <utility>
99

10+
// CXX_HAVE_STD_STRING_VIEW
11+
//
12+
// Checks whether C++17 std::string_view is available.
13+
#ifdef CXX_HAVE_STD_STRING_VIEW
14+
#error "CXX_HAVE_STD_STRING_VIEW cannot be directly set."
15+
#endif
16+
17+
#ifdef __has_include
18+
#if __has_include(<string_view>) && __cplusplus >= 201703L
19+
#define CXX_HAVE_STD_STRING_VIEW 1
20+
#endif
21+
#endif
22+
23+
// For MSVC, `__has_include` is supported in VS 2017 15.3, which is later than
24+
// the support for <optional>, <any>, <string_view>, <variant>. So we use
25+
// _MSC_VER to check whether we have VS 2017 RTM (when <optional>, <any>,
26+
// <string_view>, <variant> is implemented) or higher. Also, `__cplusplus` is
27+
// not correctly set by MSVC, so we use `_MSVC_LANG` to check the language
28+
// version.
29+
#if defined(_MSC_VER) && _MSC_VER >= 1910 && \
30+
((defined(_MSVC_LANG) && _MSVC_LANG > 201402) || __cplusplus > 201402)
31+
#define CXX_HAVE_STD_STRING_VIEW 1
32+
#endif
33+
34+
#ifdef CXX_HAVE_STD_STRING_VIEW
35+
#include <string_view>
36+
#endif
37+
1038
namespace rust {
1139
inline namespace cxxbridge02 {
1240

@@ -50,8 +78,13 @@ class Str final {
5078
Str() noexcept;
5179
Str(const Str &) noexcept;
5280

81+
#ifdef CXX_HAVE_STD_STRING_VIEW
82+
Str(std::string_view);
83+
#else
5384
Str(const std::string &);
5485
Str(const char *);
86+
#endif
87+
5588
Str(std::string &&) = delete;
5689

5790
Str &operator=(Str) noexcept;

0 commit comments

Comments
 (0)