<i id='K7sks'><tr id='K7sks'><dt id='K7sks'><q id='K7sks'><span id='K7sks'><b id='K7sks'><form id='K7sks'><ins id='K7sks'></ins><ul id='K7sks'></ul><sub id='K7sks'></sub></form><legend id='K7sks'></legend><bdo id='K7sks'><pre id='K7sks'><center id='K7sks'></center></pre></bdo></b><th id='K7sks'></th></span></q></dt></tr></i><div id='K7sks'><tfoot id='K7sks'></tfoot><dl id='K7sks'><fieldset id='K7sks'></fieldset></dl></div>
  • <tfoot id='K7sks'></tfoot>
  • <legend id='K7sks'><style id='K7sks'><dir id='K7sks'><q id='K7sks'></q></dir></style></legend>
    1. <small id='K7sks'></small><noframes id='K7sks'>

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

      1. 如何扩展词法转换以支持枚举类型?

        时间:2023-06-04
        <legend id='xBkfr'><style id='xBkfr'><dir id='xBkfr'><q id='xBkfr'></q></dir></style></legend>
          <bdo id='xBkfr'></bdo><ul id='xBkfr'></ul>

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

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

                  本文介绍了如何扩展词法转换以支持枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下函数可以将字符串转换为数字数据类型:

                  I have the following function that will convert a string into a numeric data type:

                  template <typename T>
                  bool ConvertString(const std::string& theString, T& theResult)
                  {
                      std::istringstream iss(theString);
                      return !(iss >> theResult).fail();
                  }
                  

                  然而,这不适用于枚举类型,所以我做了这样的事情:

                  This does not work for enumerated types, however, so I have done something like this:

                  template <typename T>
                  bool ConvertStringToEnum(const std::string& theString, T& theResult)
                  {
                      std::istringstream iss(theString);
                      unsigned int temp;
                      const bool isValid = !(iss >> temp).fail();
                      theResult = static_cast<T>(temp);
                      return isValid;
                  }
                  

                  (我假设 theString 具有枚举类型的有效值;我主要将其用于简单的序列化)

                  (I'm making the assumption that theString has a valid value for the enumerated type; I'm using this mainly for simple serialization)

                  有没有办法创建一个结合了这两个功能的函数?

                  Is there a way to create a single function that combines both of these?

                  我对模板参数进行了一些尝试,但没有想出任何东西;不必为枚举类型调用一个函数而为其他所有类型调用另一个函数就好了.

                  I've played a bit with the template arguments but haven't come up with anything; it'd just be nice not to have to call one function for enumerated types and another for everything else.

                  谢谢

                  推荐答案

                  你必须做两个步骤.找到一个足够大的整数类型来存储值.您可以使用 unsigned long,但值可能为负.然后你可以使用 long 但这些值可以扩展到 unsigned long 的范围内.因此,并没有真正适合所有人的类型.

                  You have to do two steps. Finding an integral type large enough to store the values. You could use unsigned long, but the values could be negative. Then you could use long but the values could extend into the range of unsigned long. So there is not really a fit-it-all type.

                  不过有一个技巧,通过使用重载解析.这是它

                  There is a trick though, by using overload resolution. Here is it

                  template<typename T>
                  struct id { typedef T type; };
                  
                  id<char[1]>::type &find_etype(int);
                  id<char[2]>::type &find_etype(unsigned int);
                  id<char[3]>::type &find_etype(long);
                  id<char[4]>::type &find_etype(unsigned long);
                  

                  如果您的实现支持,您可以适当地更改它以涵盖 long longunsigned long long.现在,传递一个枚举类型会比所有其他类型更喜欢这些 - 这是一种可以存储它的所有值的类型.您只需要将返回类型的 sizeof 传递给某个模板.

                  You can change it appropriately to cover also long long or unsigned long long if your implementation has support for that. Now, passing an enum type will prefer one of these over all the other ones - that's a type that can store all values of it. You just need to pass sizeof of the return type to some template.

                  template<int> struct get_etype;
                  template<> struct get_etype<1> { typedef int type; };
                  template<> struct get_etype<2> { typedef unsigned int type; };
                  template<> struct get_etype<3> { typedef long type; };
                  template<> struct get_etype<4> { typedef unsigned long type; };
                  

                  现在,您可以获得正确的类型.您现在需要做的就是查看某个类型是否是枚举.如何做到这一点在C++ 模板 - 完整指南"一书中有描述,不幸的是代码很多.所以我会使用 boost 的 is_enum.把它放在一起,它可能看起来像

                  Now, you can get a right type. All you need now is to see whether some type is an enumeration. How to do this is described in the book "C++ Templates - The complete Guide", and unfortunately is a whole lot of code. So i would use boost's is_enum. Putting it together, it could look like

                  template <typename T>
                  typename boost::disable_if< boost::is_enum<T>, bool>::type 
                  ConvertString(const std::string& theString, T& theResult)
                  {
                      std::istringstream iss(theString);
                      return !(iss >> theResult).fail();
                  }
                  
                  template <typename T>
                  typename boost::enable_if< boost::is_enum<T>, bool>::type 
                  ConvertString(const std::string& theString, T& theResult)
                  {
                      typedef typename get_etype<sizeof find_etype(theResult)>::type 
                        safe_type;
                  
                      std::istringstream iss(theString);
                      safe_type temp;
                      const bool isValid = !(iss >> temp).fail();
                      theResult = static_cast<T>(temp);
                      return isValid;
                  }
                  

                  希望这会有所帮助.

                  这篇关于如何扩展词法转换以支持枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C++ 中生成 xml 的最简单方法是什么? 下一篇:如何使用 Boost.serialize 序列化派生模板类?

                  相关文章

                    <small id='11xiQ'></small><noframes id='11xiQ'>

                      <bdo id='11xiQ'></bdo><ul id='11xiQ'></ul>

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

                    1. <tfoot id='11xiQ'></tfoot>