透明C ++编号/ Int包装 [英] Transparent C++ Number/Int Wrapper

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

问题描述

我已经做了一个类用于无符号整数(8,16,32 ...)的位,我一直试图基准它与裸露的逐位操作,但得到一个准确的速度测试的位运算是最复杂的。包装器的原因是它的使用复杂得多。



现在,虽然这可能在学术上比实际有用,我想知道它是否有可能使一个类绕过一个int是透明的,它就像直接使用int(所有可能的操作符)除了允许我以某些方式操作它的函数,如果可能是快速的(有很多内联)。



我可以使用任何和所有的C ++ 0x功能来实现这一点。


<您可以设置一个自定义类并重载所有相关的运算符(算术运算符,比较运算符,按位运算符,转换运算符),并根据需要提供自己的行为。



据我所知,确实可以使它在几乎所有方面都表现得像一个 int



一个内联编译器可能会在消除大部分开销方面做得很好,但是要想知道结果有多好,你需要尝试一下。



这种类型对基准测试没有什么用处,但我宁愿重复相同的操作,直到测量的相对误差变小为止。它或多或少是不可能的时间的速度像一个单一的逐位操作,因为它真的归结为一个或两个机器指令。


I've made a class for working with bits for unsigned int(8, 16, 32...), and I've been trying to benchmark it compared to the bare bitwise operations but getting an accurate test of the speed of bitwise operations is complicated to say the least. The reason for the wrapper is that it is a lot less complicated to use.

Now, while this may be more academically useful than practically, I would like to know if it was possible to make a class wrapping around an int that is so transparent that it is like working with the int directly (with all possible operators) except with functions that let me manipulate it in certain ways, and if it is possible to be as fast (with a lot of inlining).

I'm okay with using any and all C++0x features to achieve this.

解决方案

You can setup a custom class and overload all the relevant operators (arithmetic operators, comparison operators, bitwise operators, conversion operators) and supply your own behaviour as needed.

As far as I know, it would indeed be possible to make it behave like an int in almost all respects.

An inlining compiler will presumably do a very good job at removing most of the overhead, but to find out how good the results are, you will need to try it out.

Such a type would have little use for benchmarking, though - I'd rather repeat the very same operations until the relative error in your measurements get smaller. It's more or less impossible to time the speed of a something like a single bitwise operation as it really boils down to one or two machine instructions.

这篇关于透明C ++编号/ Int包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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