这个 absl::StrCat 错误在哪里发生,在 Abseil 或 MSVC 中?
Where does this absl::StrCat bug occur, in Abseil or MSVC?
I 从字符数组创建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/abseilabsl/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/abseilabsl/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/abseilabsl/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
除了 MSVC 中似乎缺少operator""sv
, 第 18 行和第 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/abseilabsl/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 FilesC++ Sourceabseil-cppabsl/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 Studio2017BuildToolsVCToolsMSVC14.13.26128includexstring(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)
^
在当前的 MSVCincludexstring
中,似乎不可能从std::_String_view_iterator<std::char_traits<char> >
转换为const std::basic_string_view<char, std::char_traits<char> >::const_pointer
(又名const char *const
Clang和GCC对此很满意。
戈德博尔特
更新
根据Algirdas Preidžius和Hans Passant下面的评论,将godbolt编译器从MSVC 2017 RTM更改为MSVC Pre 2018后,sv
文字现在都是OK键。
上面还提到了一个错误。
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/abseilabsl/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])'
金霹雳 2
可以从absl::string_view
创建absl::AlphaNum
。但不是来自std::string_view
.您的代码无法生成,因为目前这些是不同的string_view
类型。
原因是在撰写此答案时,功能测试宏ABSL_HAVE_STD_STRING_VIEW
已被注释掉。在该宏下,absl::string_view
代表std::string_view
.在宏下,将生成代码。因此,可以说"错误"在于Abesil团队尚未更新库。
目前,面向未来的解决方案是使用absl::string_view
.
- MSVC多行宏编译器错误
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- MSVC是否支持C++11样式的属性而不是__declspec
- MSVC将仅移动结构参数解释为指针
- C++17中函数模板中的静态数组初始化(MSVC 2019)
- 有与__builtin__FUNCTION()等效的MSVC吗
- 为什么 gcc 编译这个而 msvc 没有
- 看起来is_nothrow_constructible_v()在MSVC中被破坏了,我错了吗
- 正在解码MSVC 32位版本的程序集(作业).没有手术做什么
- MSVC 忽略 [[maybe_unused]] 的功能?
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- MSVC中的宏观扩展问题
- "Ill-defined for-loop - loop executes infinitely" (MSVC C6295)
- __CHAR_BIT__宏是否为 MSVC 编译器预定义?
- 不正确的操作数类型 MSVC
- 使用 MSVC 调试类型
- CRTP 中的复制赋值运算符 - gcc vs clang 和 msvc
- MSVC(Visual C++)是否有等效的-weffc ++?
- 命名空间范围内的外部 - GCC vs clang vs msvc
- 这个 absl::StrCat 错误在哪里发生,在 Abseil 或 MSVC 中?