0

I creat string_view from char array

  // creat sv from vector;
  std::vector<char> vec = { 'w', 'h', 'a', 't' };
  char* char_ptr = vec.data();
  size_t sz = vec.size();
  std::string_view sview_obj = std::string_view(char_ptr, sz);

If you write:

  using namespace std::string_view_literals;

  std::cout << absl::StrCat(sview_obj, "ever")    // Error
            << absl::StrCat(sview_obj, sview_obj) // Error
            << absl::StrCat("what", "ever"sv)     // Error
            << absl::StrCat("what"sv, "ever"sv)   // Error
            << absl::StrCat("what", "ever");      // Okey

MSVC error:

<source>(16): error C2039: 'string_view_literals': is not a member of 'std'
/opt/compiler-explorer/libs/abseil\absl/numeric/int128.h(231): note: see declaration of 'std'
<source>(16): error C2871: 'string_view_literals': a namespace with this name does not exist
<source>(18): error C2665: 'absl::StrCat': none of the 5 overloads could convert all the argument types
/opt/compiler-explorer/libs/abseil\absl/strings/str_cat.h(310): note: could be 'std::string absl::StrCat(const absl::AlphaNum &,const absl::AlphaNum &)'
<source>(18): note: while trying to match the argument list '(std::string_view, const char [5])'
<source>(19): error C2665: 'absl::StrCat': none of the 5 overloads could convert all the argument types
/opt/compiler-explorer/libs/abseil\absl/strings/str_cat.h(310): note: could be 'std::string absl::StrCat(const absl::AlphaNum &,const absl::AlphaNum &)'
<source>(19): note: while trying to match the argument list '(std::string_view, std::string_view)'
<source>(20): error C3688: invalid literal suffix 'sv'; literal operator or literal operator template 'operator ""sv' not found
<source>(21): error C3688: invalid literal suffix 'sv'; literal operator or literal operator template 'operator ""sv' not found

Other than operator""sv seems missing in MSVC, line 18 and 19 have a thing in common:

  std::cout << absl::StrCat(sview_obj, "ever")    // Error
            << absl::StrCat(sview_obj, sview_obj) // Error

Error:

<source>(18): error C2665: 'absl::StrCat': none of the 5 overloads could convert all the argument types
/opt/compiler-explorer/libs/abseil\absl/strings/str_cat.h(310): note: could be 'std::string absl::StrCat(const absl::AlphaNum &,const absl::AlphaNum &)'
<source>(18): note: while trying to match the argument list '(std::string_view, const char [5])'

When I compile on my VC BuildTool (with clang 8 target msvc), it gives additional clues here.

In file included from C:/Program Files/C++ Source/abseil-cpp/absl/strings/ascii.cc:15:
C:\Program Files\C++ Source\abseil-cpp\absl/strings/ascii.h(198,10):  error: no matching constructor for initialization of 'absl::string_view' (aka 'basic_string_view<char>')
  return absl::string_view(it, str.end() - it);
         ^                 ~~~~~~~~~~~~~~~~~~
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.13.26128\include\xstring(809,12):  note: candidate constructor not viable: no known conversion from 'std::_String_view_iterator<std::char_traits<char> >' to 'const std::basic_string_view<char, std::char_traits<char> >::const_pointer' (aka 'const char *const') for 1st argument
        constexpr basic_string_view(_In_reads_(_Count) const const_pointer _Cts, const size_type _Count)
                  ^

It seems conversion from std::_String_view_iterator<std::char_traits<char> > to const std::basic_string_view<char, std::char_traits<char> >::const_pointer (aka const char *const) is impossible in current MSVC include\xstring

Clang and gcc are okey with this.

godbolt


Update

According to Algirdas Preidžius and Hans Passant's comment below, after change godbolt compiler from MSVC 2017 RTM into MSVC Pre 2018, sv literals are now all okey.

Still there is one more bug mentioned above.

std::cout << absl::StrCat(sview_obj, "ever")    // Error

Error

<source>(18): error C2665: 'absl::StrCat': none of the 5 overloads could convert all the argument types
/opt/compiler-explorer/libs/abseil\absl/strings/str_cat.h(310): note: could be 'std::string absl::StrCat(const absl::AlphaNum &,const absl::AlphaNum &)'
<source>(18): note: while trying to match the argument list '(std::string_view, const char [5])'

goldbolt 2

sandthorn
  • 2,770
  • 1
  • 15
  • 59
  • 1
    1) Note, correct namespace is `std::literals::string_view_literals`. 2) Related: https://developercommunity.visualstudio.com/content/problem/17172/c17-stdstring-view-does-not-have-the-stdliteralsst.html – Algirdas Preidžius Aug 22 '18 at 12:08
  • 1
    You need to change a setting, neither godbolt nor your VS version seem to be aware yet that C++17 was approved. Use Project > Properties > C/C++ > Language > "C++ Language Standard" = ISO C++17 Standard (/std:c++17). – Hans Passant Aug 22 '18 at 12:11
  • @HansPassant thanks I updated question. Still there is one more bug error left. – sandthorn Aug 22 '18 at 12:25
  • @AlgirdasPreidžius Thanks. There is still one more bug left here. – sandthorn Aug 22 '18 at 12:26

1 Answers1

1

absl::AlphaNum can be created from an absl::string_view. But not from std::string_view. Your code cannot build because at the moment those are distinct string_view types.

The reason is that as of writing this answer, the feature test macro ABSL_HAVE_STD_STRING_VIEW is commented out. Under that macro, absl::string_view stands for std::string_view. Under the macro, your code would build. So one can say the "fault" is with the Abesil team for not updating the library yet.

Right now, the future proof solution is to use absl::string_view.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Thanks. Unfortunately, they just *intend* to comment out line [424](https://github.com/abseil/abseil-cpp/blob/fefc83638fb69395d259ed245699310610429064/absl/base/config.h#L424) perhaps because of MSVC incompatiblity of aliasing `absl::string_view` to `std::string_view`. You are totally right `absl::string_view` which they intend not to alias `string_view` on MSVC just work => [godbolt](https://godbolt.org/z/yHnQ7B). However my clang-target-msvc still has issue which is the same issue if on aliasing `std::string_view` (comment out 424 doesn't help) – sandthorn Aug 22 '18 at 18:29