nsh*_*hct 10 c++ string-view c++17
在实现C++ 1z std::basic_string_view以在较旧的编译器上使用它时,我遇到了流输出操作符重载的问题.基本上,它必须输出string_viewwhile 引用的内容而不依赖于任何存在的null-terminator(因为string_view不保证是以null终止的).
通常,编写重载operator<<非常容易,因为您可以依赖已经存在的重载,因此不需要使用SO中此问题中提到的 sentry对象.
但在这种情况下,没有预定义的重载来operator<<获取字符指针和长度(显然).因此,我std::string在当前的实现中创建了一个临时实例:
template< typename TChar, typename TTraits >
auto operator<<(::std::basic_ostream<TChar, TTraits>& p_os, basic_string_view<TChar, TTraits> p_v)
-> ::std::basic_ostream<TChar, TTraits>&
{
p_os << p_v.to_string(); // to_string() returns a ::std::string.
return p_os;
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我真的不喜欢我必须创建一个临时std::string实例的事实,因为这需要冗余地复制数据和动态内存的潜在用法.至少在我看来,这违背了使用轻量级引用类型的目的.
所以我的问题是:
在没有开销的情况下为string_view实现正确格式化输出的最佳方法是什么?
在研究时,我发现LLVM是这样的:(在这里找到)
// [string.view.io]
template<class _CharT, class _Traits>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv)
{
return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
}
Run Code Online (Sandbox Code Playgroud)
__put_character_sequence驻留在此文件中的实现,但它大量使用内部函数来进行格式化.我需要自己重新实现所有格式吗?
据我所知,你必须自己处理.
幸运的是,您需要为类似字符串的项目执行的格式化相当小 - 如果需要,大多数在字符串之前或之后插入填充.
ios_base::width().ios_base::fmtflags().ios_base::fill().fixed标志 - 如果内存服务,设置它,你需要截断你的字符串,如果它比当前字段宽度长.所以(使用超简化的实现string_view),代码可能看起来像这样:
#include <iostream>
#include <iomanip>
#include <ios>
#include <sstream>
class string_view {
char const *data;
size_t len;
public:
string_view(char const *data, size_t len) : data(data), len(len) {}
friend std::ostream &operator<<(std::ostream &os, string_view const &sv) {
std::ostream::sentry s{ os };
if (s) {
auto fill = os.fill();
auto width = os.width();
bool left = os.flags() & std::ios::left;
bool right = os.flags() & std::ios::right;
bool fixed = os.flags() & std::ios::fixed;
auto pad = [&](size_t width) { while (width--) os.put(fill); };
if (sv.len < width) {
auto padding_len = width - sv.len;
if (right) pad(padding_len);
os.write(sv.data, sv.len);
if (left) pad(padding_len);
}
else {
os.write(sv.data, fixed ? width : sv.len);
}
}
os.width(0);
return os;
}
};
#ifdef TEST
void check(std::stringstream &a, std::stringstream &b) {
static int i;
++i;
if (a.str() != b.str()) {
std::cout << "Difference in test:" << i << "\n";
std::cout << "\"" << a.str() << "\"\n";
std::cout << "\"" << b.str() << "\"\n";
}
a.seekp(0);
b.seekp(0);
}
int main() {
char string[] = "Now is the time for every good man to come to the aid of Jerry.";
std::stringstream test1;
std::stringstream test2;
test1 << string_view(string, 3);
test2 << std::string(string, 3);
check(test1, test2);
test1 << string_view(string + 4, 2);
test2 << string_view(string + 4, 2);
check(test1, test2);
test1 << std::setw(10) << std::left << string_view(string, 6);
test2 << std::setw(10) << std::left << std::string(string, 6);
check(test1, test2);
test1 << std::setw(10) << std::right << string_view(string, 6);
test2 << std::setw(10) << std::right << std::string(string, 6);
check(test1, test2);
test1 << std::setw(10) << std::right << string_view(string, sizeof(string));
test2 << std::setw(10) << std::right << std::string(string, sizeof(string));
check(test1, test2);
test1 << std::setw(10) << std::right << std::fixed << string_view(string, sizeof(string));
test2 << std::setw(10) << std::right << std::fixed << std::string(string, sizeof(string));
check(test1, test2);
}
#endif
Run Code Online (Sandbox Code Playgroud)
哦 - 还有一个细节.由于我们只是写入流,而不是直接写入底层缓冲区,我认为sentry在这种情况下我们可能实际上不需要创建对象.如图所示,创建和使用它非常简单,但毫无疑问,删除后它至少会快一点点.