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

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

      2. 围绕基本类型的 C++ 类包装器

        C++ Class wrapper around fundamental types(围绕基本类型的 C++ 类包装器)

          1. <small id='xXY9j'></small><noframes id='xXY9j'>

            • <i id='xXY9j'><tr id='xXY9j'><dt id='xXY9j'><q id='xXY9j'><span id='xXY9j'><b id='xXY9j'><form id='xXY9j'><ins id='xXY9j'></ins><ul id='xXY9j'></ul><sub id='xXY9j'></sub></form><legend id='xXY9j'></legend><bdo id='xXY9j'><pre id='xXY9j'><center id='xXY9j'></center></pre></bdo></b><th id='xXY9j'></th></span></q></dt></tr></i><div id='xXY9j'><tfoot id='xXY9j'></tfoot><dl id='xXY9j'><fieldset id='xXY9j'></fieldset></dl></div>
                <tbody id='xXY9j'></tbody>
              <legend id='xXY9j'><style id='xXY9j'><dir id='xXY9j'><q id='xXY9j'></q></dir></style></legend>
              1. <tfoot id='xXY9j'></tfoot>
                  <bdo id='xXY9j'></bdo><ul id='xXY9j'></ul>
                  本文介绍了围绕基本类型的 C++ 类包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我见过/使用过的许多库都有 typedef 来提供可移植的、固定大小的变量,例如 int8、uint8、int16、uint16 等,无论平台如何,它们的大小都是正确的(而 c++11 本身使用标头 stdint.h)

                  最近在我正在编写的一个小型库中使用二进制文件 i/o 后,我可以看到以这种方式使用 typedef 以确保代码可移植的好处.

                  但是,如果我要输入namespace::uint32"而不是使用内置的基本类型遇到麻烦,我也可以使替换尽可能有用.因此我正在考虑使用类而不是简单的 typedef.

                  这些包装类将实现所有普通运算符,因此可以与基本类型互换使用.

                  例如:

                  int x = 0;//做东西

                  可能变成

                  class intWrapper {//任何};intWrapper = 0;//做东西

                  无需修改//do stuff"中的任何代码

                  我考虑这种方法而不只是 typedef 的原因是我已经有了对基本类型进行操作的函数,例如

                  std::string numberToString(double toConvert);std::string numberToHexString(double toConvert);int intToXSignificantPlaces(const int& number,无符号字符 numberOfSignificantPlaces);bool numbersAreApproximatelyEqual(float tolerance);//等等....

                  从语法上讲,执行以下操作会更好(也更糟糕):

                  intWrapper.toString();intWrapper.toHexString();//等等

                  它还允许我实现 bigint 类(int128 等)并让这些类和较小的类(基于基本类型)使用相同的接口.

                  最后,每个包装器都可以有一个自己的静态实例,称为 max 和 min,所以 int32::max 和 int32::min 的好语法是可能的.

                  但是,在执行此操作之前,我想解决一些问题(因为它主要是语法糖,并且这些类型会被使用,因此通常会使用任何额外的开销可能会对性能产生重大影响).

                  1) 在使用 someClass.operator+()、someClass.operator-() 等时,是否有任何额外的函数调用开销,而不是仅使用 int a + int b?如果是这样,内联 operator+() 会消除所有这些开销吗?

                  2) 所有外部函数都需要原始类型,例如 glVertex3f(float, float, float) 不能简单地传递 3 个 floatWrapper 对象,有没有办法让编译器自动将 floatWrapper 转换为 float?如果是,是否会影响性能?

                  3) 是否有额外的内存开销?我理解(?)具有继承的类具有某种虚拟表指针,因此使用稍微更多的内存(或者只是用于虚拟函数?),但假设这些包装类不是继承自/不是子类,则没有使用类而不是基本类型是否有任何额外的内存使用?

                  4) 这是否会导致任何其他问题/性能影响?

                  解决方案

                  1) 使用 someClass.operator+() 时是否有任何额外的函数调用开销

                  不,如果函数体很小并且在头中,它将被内联,并且没有开销

                  <块引用>

                  2) 有没有办法让编译器自动将 floatWrapper 转换为浮点数?

                  struct floatWrapper {浮动包装器(浮动);//从浮点数隐式转换运算符浮动();//隐式转换为浮点数.};

                  同样,如果函数体很小并且在头中,它将被内联,并且没有开销

                  <块引用>

                  3) 是否有额外的内存开销?

                  如果没有虚函数就不行.如果一个类声明或继承了任何虚函数,它就被称为多态.如果类不是多态的,则对象不需要包含指向虚函数表的指针.此外,不允许在继承层次结构中将指向非多态类的指针/引用动态转换为指向派生类的指针/引用,因此对象不需要具有某种类型信息.<块引用>

                  4) 这是否会导致任何其他问题/性能影响?

                  性能?号

                  另外,确保将不修改 lhs 的二元运算符实现为自由函数,并重载它们以支持 floatWrapperfloat 的所有相关排列.

                  struct floatWrapper {显式 floatWrapper(float);运算符浮动();//隐式转换为浮点数.floatWrapper 运算符-=(float);};floatWrapper 运算符-(floatWrapper lhs, floatWrapper rhs){返回 lhs-=rhs;}floatWrapper 运算符-(float lhs, floatWrapper rhs){return floatWrapper(lhs)-=rhs;}floatWrapper 运算符-(floatWrapper lhs, float rhs){返回 lhs-=rhs;}

                  他的尝试.请注意,您需要一个稍微不同的 float/double/long double 版本.

                  Many libraries I have seen/used have typedefs to provide portable, fixed size variables, eg int8, uint8, int16, uint16, etc which will be the correct size regardless of platform (and c++11 does it itself with the header stdint.h)

                  After recently using binary file i/o in a small library I'm writing I can see the benefit of using typedefs in this way to ensure the code is portable.

                  However, if I'm going to the trouble of typing "namespace::uint32" rather than using built in fundamental types, I may as well make the replacement as useful as possible. Therefore I am considering using classes instead of simple typedefs.

                  These wrapper classes would implement all normal operators so could be used interchangeably with the fundamental type.

                  Eg:

                  int x = 0;
                  //do stuff
                  

                  could become

                  class intWrapper {
                  //whatever
                  };
                  
                  intWrapper = 0;
                  //do stuff
                  

                  without having to modify any code in "//do stuff"

                  The reason I'm considering this approach as opposed to just typedefs is the fact I already have functions that operate on fundamental types, eg

                  std::string numberToString(double toConvert);
                  
                  std::string numberToHexString(double toConvert);
                  
                  int intToXSignificantPlaces(const int& number, 
                                                 unsigned char numberOfSignificantPlaces);
                  
                  bool numbersAreApproximatelyEqual(float tollerance);
                  //etc....
                  

                  Syntactically it would be nicer (and more oop) to do the following:

                  intWrapper.toString();
                  intWrapper.toHexString();
                  //etc
                  

                  Also it would allow me to implement bigint classes (int128, etc) and have those and the smaller ones (based on fundamental types) use identical interfaces.

                  Finally each wrapper could have a static instance of itself called max and min, so the nice syntax of int32::max and int32::min would be possible.

                  However, I have a few concerns that I would like to address before doing this (since it is mostly syntactical sugar and these types would be used so commonly any extra overhead could have a significant performance impact).

                  1) Is there any additional function calling overhead when using someClass.operator+(), someClass.operator-() etc over just int a + int b? If so, would inlining operator+() eliminate ALL this overhead?

                  2) All external functions require the primitive type, eg glVertex3f(float, float, float) could not simply be passed 3 floatWrapper objects, is there a way to automatically make the compiler cast the floatWrapper to a float? If so, are there performance impacts?

                  3) Is there any additional memory overhead? I understand(?) that classes with inheritance have some sort of virtual table pointer and so use slightly more memory (or is that just for virtual functions?), but assuming these wrapper classes are not inherited from/are not child classes there isn't any additional memory use using classes instead of fundamental types, is there?

                  4) Are there any other problems / performance impacts this could cause?

                  解决方案

                  1) Is there any additional function calling overhead when using someClass.operator+()

                  No, if the function body is small and in the header, it will be inlined, and have no overhead

                  2) Is there a way to automatically make the compiler cast the floatWrapper to a float?

                  struct floatWrapper {
                      floatWrapper(float); //implicit conversion from float
                      operator float(); //implicit conversion to float.  
                  };
                  

                  Again, if the body of the function is small and in the header, it will be inlined, and have no overhead

                  3) Is there any additional memory overhead?

                  not if there's no virtual functions. A class is called polymorphic if it declares or inherits any virtual functions. If a class is not polymorphic, the objects do not need to include a pointer to a virtual function table. Moreover, performing dynamic_cast of a pointer/reference to a non-polymorphic class down the inheritance hierarchy to a pointer/reference to a derived class is not allowed, so there is no need for the objects to have some kind of type information.

                  4) Are there any other problems / performance impacts this could cause?

                  performance? No.

                  Also, be sure to implement binary operators that don't modify the lhs as free functions, and overload them to support all relevant permutations of floatWrapper and float.

                  struct floatWrapper {
                      explicit floatWrapper(float);
                      operator float(); //implicit conversion to float.  
                      floatWrapper operator-=(float);
                  };
                  floatWrapper operator-(floatWrapper lhs, floatWrapper rhs) 
                  {return lhs-=rhs;}
                  floatWrapper operator-(float lhs, floatWrapper rhs) 
                  {return floatWrapper(lhs)-=rhs;}
                  floatWrapper operator-(floatWrapper lhs, float rhs) 
                  {return lhs-=rhs;}
                  

                  Here's my attempt at such a thing. Note you'll need a slightly different version for float/double/long double.

                  这篇关于围绕基本类型的 C++ 类包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                        <bdo id='foYkE'></bdo><ul id='foYkE'></ul>
                        <tfoot id='foYkE'></tfoot>

                        1. <small id='foYkE'></small><noframes id='foYkE'>

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