C ++基本类型的类包装器 [英] C++ Class wrapper around fundamental types

查看:177
本文介绍了C ++基本类型的类包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到/使用的许多库有typedef提供可移植的固定大小的变量,例如int8,uint8,int16,uint16等,将是正确的大小,不管平台(和c ++ 11它本身与头文件stdint.h)

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)

最近在一个小的库中使用二进制文件i / o我写了我可以看到使用typedefs的好处,代码是可移植的。

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.

但是,如果我打算输入namespace :: uint32而不是使用内置的基本类型,更换尽可能有用。因此,我正在考虑使用类而不是简单的typedef。

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.

int x = 0;
//do stuff

可能会成为

class intWrapper {
//whatever
};

intWrapper = 0;
//do stuff

我认为这种方法相对于typedefs的原因是我已经有了基本类型的函数,例如

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



此外,它还允许我实现bigint类(int128等)并且有那些和较小的(基于基本类型)使用相同的接口。

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.

最后,每个包装器可以有一个称为max和min的静态实例, int 32 :: max和int32 :: min的语法是可能的。

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)当使用someClass时,有额外的函数调用开销。运算符+(),someClass.operator-()等只是int a + int b?

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)所有外部函数都需要原始类型,例如glVertex3f(float,float,float)不能简单地传递3 floatWrapper对象,有没有办法自动使编译器将floatWrapper转换为float?如果是,是否有性能影响?

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)是否有额外的内存开销?我理解(?),继承类具有某种虚拟表指针,因此使用更多的内存(或只是为虚拟函数?),但假设这些包装类不是继承自/不是子类,

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)是否还有任何其他问题/性能影响可能导致?

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

推荐答案


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

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)有没有办法自动使编译器将floatWrapper转换为float?

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)是否有额外的内存开销?

3) Is there any additional memory overhead?

如果没有虚拟函数。如果一个类声明或继承任何虚拟函数,则称为多态。如果类不是多态的,则对象不需要包括指向虚拟函数表的指针。此外,不允许对继承层次结构执行指向非多态类的指针/引用的对指向引用类的指针/引用的dynamic_cast,因此不需要对象具有某种类型的信息。 p>

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)这是否会导致其他问题/性能影响?

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

性能?

此外,请务必实现不将lhs修改为自由函数的二进制运算符,并重载它们以支持所有相关的排列 floatWrapper float

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 ++基本类型的类包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆