本文介绍了具有u8,char8_t和std :: string的C ++ 20的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11为我们带来了UTF-8字面量的u8前缀,我认为几年前这很酷,并在代码中添加了以下内容:

C++11 brought us the u8 prefix for UTF-8 literals and I thought that was pretty cool a few years ago and peppered my code with things like this:

std::string myString = u8"●";

这一切都很好,但是在C ++ 20中却出现了问题似乎可以编译了,因为u8创建了char8_t *,而这与仅使用char的std :: string不兼容。

This is all fine and good, but the issue comes up in C++20 it doesn't seem to compile anymore because u8 creates a char8_t* and this is incompatible with std::string which just uses char.

我应该创建一个新的utf8string吗?在C ++ 20世界中,如果我们有更显式的类型与标准std :: string真正不匹配的显式类型,一致而正确的方法是什么?

Should I be creating a new utf8string? What's the consistent and correct way to do this kind of thing in a C++20 world where we have more explicit types that don't really match with the standard std::string?

推荐答案

除了@lubgr的答案,论文讨论了几种使用<$ c制作 std :: string 的方法。 $ c> char8_t 字符数组。

In addition to @lubgr's answer, the paper char8_t backward compatibility remediation (P1423) discusses several ways how to make std::string with char8_t character arrays.

基本上,您可以将 u8 char数组转换为普通 char数组以获取与C ++ 17和之前的行为相同,您只需要更加明确即可。本文讨论了各种方法。

Basically the idea is that you can cast the u8 char array into a "normal" char array to get the same behaviour as C++17 and before, you just have to be a bit more explicit. The paper discusses various ways to do this.

适合用例的最简单的方法(但开销不为零,除非添加更多的重载)可能是最后一种方法,即引入显式转换函数:

The most simple (but not fully zero overhead, unless you add more overloads) method that fits your usecase is probably the last one, i.e. introduce explicit conversion functions:

std::string from_u8string(const std::string &s) {
  return s;
}
std::string from_u8string(std::string &&s) {
  return std::move(s);
}
#if defined(__cpp_lib_char8_t)
std::string from_u8string(const std::u8string &s) {
  return std::string(s.begin(), s.end());
}
#endif

这篇关于具有u8,char8_t和std :: string的C ++ 20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:16