• <small id='prHDi'></small><noframes id='prHDi'>

  • <i id='prHDi'><tr id='prHDi'><dt id='prHDi'><q id='prHDi'><span id='prHDi'><b id='prHDi'><form id='prHDi'><ins id='prHDi'></ins><ul id='prHDi'></ul><sub id='prHDi'></sub></form><legend id='prHDi'></legend><bdo id='prHDi'><pre id='prHDi'><center id='prHDi'></center></pre></bdo></b><th id='prHDi'></th></span></q></dt></tr></i><div id='prHDi'><tfoot id='prHDi'></tfoot><dl id='prHDi'><fieldset id='prHDi'></fieldset></dl></div>

      <legend id='prHDi'><style id='prHDi'><dir id='prHDi'><q id='prHDi'></q></dir></style></legend>
      <tfoot id='prHDi'></tfoot>
        <bdo id='prHDi'></bdo><ul id='prHDi'></ul>

        如何在需要旧式无符号字符的地方使用新的 std

        时间:2023-09-15
        <tfoot id='MQcY6'></tfoot>
          <tbody id='MQcY6'></tbody>

            <bdo id='MQcY6'></bdo><ul id='MQcY6'></ul>

                <small id='MQcY6'></small><noframes id='MQcY6'>

                  <legend id='MQcY6'><style id='MQcY6'><dir id='MQcY6'><q id='MQcY6'></q></dir></style></legend>
                  <i id='MQcY6'><tr id='MQcY6'><dt id='MQcY6'><q id='MQcY6'><span id='MQcY6'><b id='MQcY6'><form id='MQcY6'><ins id='MQcY6'></ins><ul id='MQcY6'></ul><sub id='MQcY6'></sub></form><legend id='MQcY6'></legend><bdo id='MQcY6'><pre id='MQcY6'><center id='MQcY6'></center></pre></bdo></b><th id='MQcY6'></th></span></q></dt></tr></i><div id='MQcY6'><tfoot id='MQcY6'></tfoot><dl id='MQcY6'><fieldset id='MQcY6'></fieldset></dl></div>

                  本文介绍了如何在需要旧式无符号字符的地方使用新的 std::byte 类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  std::byte 是 C++17 中的一种新类型,它被定义为 enum class byte : unsigned char.这使得在没有适当转换的情况下无法使用它.所以,我为这种类型的向量做了一个别名来表示一个字节数组:

                  std::byte is a new type in C++17 which is made as enum class byte : unsigned char. This makes impossible to use it without appropriate conversion. So, I have made an alias for the vector of such type to represent a byte array:

                  using Bytes = std::vector<std::byte>;
                  

                  但是,不能在旧式中使用它:接受它作为参数的函数会失败,因为这种类型不能轻易转换成旧的 std::vector输入,例如,zipper 库的用法:

                  However, it is impossible to use it in old-style: the functions which accept it as a parameter fail because this type can not be easily converted to old std::vector<unsigned char> type, for example, a usage of zipper library:

                  /resourcecache/pakfile.cpp: In member function 'utils::Bytes resourcecache::PakFile::readFile(const string&)':
                  /resourcecache/pakfile.cpp:48:52: error: no matching function for call to 'zipper::Unzipper::extractEntryToMemory(const string&, utils::Bytes&)'
                       unzipper_->extractEntryToMemory(fileName, bytes);
                                                                      ^
                  In file included from /resourcecache/pakfile.hpp:13:0,
                                   from /resourcecache/pakfile.cpp:1:
                  /projects/linux/../../thirdparty/zipper/zipper/unzipper.h:31:10: note: candidate: bool zipper::Unzipper::extractEntryToMemory(const string&, std::vector<unsigned char>&)
                       bool extractEntryToMemory(const std::string& name, std::vector<unsigned char>& vec);
                            ^~~~~~~~~~~~~~~~~~~~
                  /projects/linux/../../thirdparty/zipper/zipper/unzipper.h:31:10: note:   no known conversion for argument 2 from 'utils::Bytes {aka std::vector<std::byte>}' to 'std::vector<unsigned char>&'
                  

                  我曾尝试执行幼稚的转换,但这也无济于事.那么,如果它被设计为有用的,它在旧的上下文中真的有用吗?我看到的唯一方法是使用 std::transform 在这些地方使用新的字节向量:

                  I have tried to perform naive casts but this does not help also. So, if it is designed to be useful, will it be actually useful in old contexts? The only method I see is to use std::transform for using new vector of bytes in these places:

                  utils::Bytes bytes;
                  std::vector<unsigned char> rawBytes;
                  unzipper_->extractEntryToMemory(fileName, rawBytes);
                  std::transform(rawBytes.cbegin(),
                                 rawBytes.cend(),
                                 std::back_inserter(bytes),
                                 [](const unsigned char c) {
                                     return static_cast<std::byte>(c);
                                 });
                  return bytes;
                  

                  是:

                  1. 丑.
                  2. 占用了很多无用的行(可以重写,但仍然需要在之前编写:)).
                  3. 复制内存,而不是仅仅使用已经创建的 rawBytes 块.

                  那么,如何在老地方使用它?

                  So, how to use it in old places?

                  推荐答案

                  您忽略了 std::byte 最初被发明的原因.发明它的原因是在内存中保存一个原始字节而不假设它是一个字符.您可以在 cppreference 中看到这一点.

                  You're missing the point why std::byte was invented in the first place. The reason it was invented is to hold a raw byte in memory without the assumption that it's a character. You can see that in cppreference.

                  与 char 和 unsigned char 一样,它可以用来访问被其他对象(对象表示)占用的原始内存,但与那些类型不同的是,它不是字符类型,也不是算术类型.

                  Like char and unsigned char, it can be used to access raw memory occupied by other objects (object representation), but unlike those types, it is not a character type and is not an arithmetic type.

                  请记住,出于安全考虑,C++ 是一种强类型语言(因此隐式转换在许多情况下受到限制).含义:如果从 bytechar 的隐式转换是可能的,那么它就会失败.

                  Remember that C++ is a strongly typed language in the interest of safety (so implicit conversions are restricted in many cases). Meaning: If an implicit conversion from byte to char was possible, it would defeat the purpose.

                  因此,要回答您的问题:要使用它,您必须在任何时候对其进行分配:

                  So, to answer your question: To use it, you have to cast it whenever you want to make an assignment to it:

                  std::byte x = (std::byte)10;
                  std::byte y = (std::byte)'a';
                  std::cout << (int)x << std::endl;
                  std::cout << (char)y << std::endl;
                  

                  按照设计,其他任何事情都行不通!所以这个转换很难看,同意,但如果你想存储字符,那么使用 char.不要使用字节,除非您想存储原始内存默认情况下不应将其解释为 char.

                  Anything else shall not work, by design! So that transform is ugly, agreed, but if you want to store chars, then use char. Don't use bytes unless you want to store raw memory that should not be interpreted as char by default.

                  而且您问题的最后一部分通常是不正确的:您不必复制,因为您不必复制整个向量.如果您暂时需要将 byte 读取为 char,只需在需要将其用作 的地方 static_cast 即可字符.它不花钱,而且是类型安全的.

                  And also the last part of your question is generally incorrect: You don't have to make copies, because you don't have to copy the whole vector. If you temporarily need to read a byte as a char, simply static_cast it at the place where you need to use it as a char. It costs nothing, and is type-safe.

                  <小时>至于你在关于将 std::vector 转换为 std::vector 的评论中的问题,你不能那样做.但是您可以使用下面的原始数组.所以,下面有一个类型 (char*):


                  As to your question in the comment about casting std::vector<char> to std::vector<std::byte>, you can't do that. But you can use the raw array underneath. So, the following has a type (char*):

                  std::vector<std::byte> bytes;
                  // fill it...
                  char* charBytes = reinterpret_cast<char*>(bytes.data()); 
                  

                  它的类型为 char*,它是指向数组第一个元素的指针,无需复制即可取消引用,如下所示:

                  This has type char*, which is a pointer to the first element of your array, and can be dereferenced without copying, as follows:

                  std::cout << charBytes[5] << std::endl; //6th element of the vector as char
                  

                  以及您从 bytes.size() 获得的大小.这是有效的,因为 std::vector 在内存中是连续的.您通常无法使用任何其他标准容器(双端队列、列表等...)执行此操作.

                  And the size you get from bytes.size(). This is valid, since std::vector is contiguous in memory. You can't generally do this with any other std container (deque, list, etc...).

                  虽然这是有效的,但它从等式中消除了部分安全性,请记住这一点.如果你需要char,不要使用byte.

                  While this is valid, it removes part of the safety from the equation, keep that in mind. If you need char, don't use byte.

                  这篇关于如何在需要旧式无符号字符的地方使用新的 std::byte 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 将多种类型推送到向量上 下一篇:如何逐个添加两个 STL 向量的元素?

                  相关文章

                  最新文章

                1. <small id='9YGjq'></small><noframes id='9YGjq'>

                    • <bdo id='9YGjq'></bdo><ul id='9YGjq'></ul>

                  1. <tfoot id='9YGjq'></tfoot>

                  2. <i id='9YGjq'><tr id='9YGjq'><dt id='9YGjq'><q id='9YGjq'><span id='9YGjq'><b id='9YGjq'><form id='9YGjq'><ins id='9YGjq'></ins><ul id='9YGjq'></ul><sub id='9YGjq'></sub></form><legend id='9YGjq'></legend><bdo id='9YGjq'><pre id='9YGjq'><center id='9YGjq'></center></pre></bdo></b><th id='9YGjq'></th></span></q></dt></tr></i><div id='9YGjq'><tfoot id='9YGjq'></tfoot><dl id='9YGjq'><fieldset id='9YGjq'></fieldset></dl></div>
                      <legend id='9YGjq'><style id='9YGjq'><dir id='9YGjq'><q id='9YGjq'></q></dir></style></legend>