c++ - 这个 absl::StrCat 错误发生在 Abseil 还是 MSVC 中?

标签 c++ visual-c++ c++17 abseil

我从字符数组创建 string_view

  // 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);

如果你写:

  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 错误:

<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

operator""sv 除外似乎在 MSVC 中丢失了, line 18 and 19有一个共同点:

  std::cout << absl::StrCat(sview_obj, "ever")    // Error
            << absl::StrCat(sview_obj, sview_obj) // 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])'

当我在我的 VC BuildTool 上编译时(使用 clang 8 目标 msvc),它在这里提供了额外的线索。

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)
                  ^

似乎是从std::_String_view_iterator<std::char_traits<char> > 转换过来的至 const std::basic_string_view<char, std::char_traits<char> >::const_pointer (又名 const char *const )在当前 MSVC 中是不可能的 include\xstring

Clang 和 gcc 对此很满意。

godbolt


更新

根据 Algirdas PreidžiusHans Passant 下面的评论,将 Godbolt 编译器从 MSVC 2017 RTM 更改为 MSVC Pre 2018 后,sv文字现在一切正常。

还有一个上面提到的bug。

std::cout << absl::StrCat(sview_obj, "ever")    // 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

最佳答案

absl::AlphaNum 可以从 absl::string_view 创建。但不是来自 std::string_view。您的代码无法构建,因为目前它们是不同的 string_view 类型。

原因是在撰写此答案时,功能测试宏 ABSL_HAVE_STD_STRING_VIEW 已被注释掉。在那个宏下, absl::string_view stands for std::string_view 。在宏下,您的代码将构建。因此,可以说“错误”在于 Abesil 团队尚未更新库。

现在,面向 future 的解决方案是使用 absl::string_view

关于c++ - 这个 absl::StrCat 错误发生在 Abseil 还是 MSVC 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51966426/

相关文章:

c++ - 传递派生模板类的 vector

c++ - 模板实例化的原型(prototype)?

c++ - Boost 客户端无法从服务器接收消息

c++ - experimental::optional nullopt_t 构造函数

c++ - 如何在 Visual Studio 中启用 C++17 编译?

c++ - 如何在不将它们作为参数传递的情况下访问线程中的类数据成员?

c# - n 个列表的字符串的组合

c++ - Microsoft Visual C++ 2010 使用的 C++ 类型?

c++ - 确定阴阳穴位

c++ - 推导指南和注入(inject)的类名