C ++ - 通用编程 - 类型选择 [英] C++ - Generic programming - Type selection

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

问题描述

以下片段对于类型T的(假设无符号)整数返回下一个最高次幂2。这是通过重复移位来实现的。

The following fragment returns the next highest power of two for an (assumed unsigned) integer of type T. It does this by shifting the bits repeatedly.

意图和目的,在位移环路中使用的无符号类型i足够大以表示(标称)65536位数。

For all intents and purposes the unsigned type i used in the bit shifting loop is sufficiently large to represent a (nominally) 65536 bit number. Practically therefore it's fine to leave it 'as is'.

template <class T>
T supremum_2(T k) {
  if (k == T(0)) return T(1);
  k--;  
  for (int i=1; i<sizeof(T)*8; i++) k |= k >> i;
  return k+1;
}

要做专业工作,循环计数器的类型应选择编译时间,以便保证能够跨越到sizeof(T)* 8而没有溢出。

To do a professional job, the type of the loop counter should be chosen at compile time so that it guarantees to be able to span up to sizeof(T)*8 without overflow.

这可以在编译时使用std :: numeric_traits?

Can this be done at compile time using std::numeric_traits? If so how?

从概念上来说,我希望能够写下如下内容:

Conceptually I would like to be able to write something like:

typedef unsigned_type_that_can_represent<sizeof(T)*8> counter_type;

...
...

for (counter_type i(1); i<sizeof(T)*8; i<<=1) k = k | k >> i;
...



基于下面的讨论,我决定添加以下上下文。

Based on the discussions below I have decided to add the following context.

换句话说:

我们如何保证选择高效)和适当类型的编译时模板代码的内部工作?如果我们发现自己在模板代码中使用具体类型,我们可能通过一个可能不透明的路由对模板的类型进行无意的假设。

How can we guarantee to select efficient (only as big as they need to be) and suitable types for the internal workings of template code at compile time? If we find ourselves using concrete types in template code we may be making inadvertent assumptions about the types of the templates through a potentially opaque route.

例如,如果我们坚持使用(比如)一个计数器的int,所有都会正常工作,直到有人使用它们的bigint库的模板代码。这可以表示具有比可以由int表示的更多二进制数字的整数。因此我们应该使类型unsigned long long?当然,只是延迟问题(虽然很长一段时间)?对于这个解决方案,有640K - 对于每个人足够大或者静态数组大小的阴影。

For example, if we were to stick with (say) an int for the counter, all will work fine until someone uses the template code with their bigint library. This could represent integers with more binary digits than can be represented by an int. Should we therefore make the type unsigned long long? Surely that just delays the problem (albeit for a long time)? There are shades of "640K - big enough for everybody" or static array sizes about this solution.

这个实例中显而易见但效率低下的选择是设置计数器的类型与数字k的类型相同。它(原则上)是低效的,因为我们只要求计数器能够表示对应于k的比特数的数。可能在其他情况下,这是错误的假设。

The obvious, but somewhat inefficient, choice in this instance is to set the type of the counter to be the same as the type of the number k. It is (in principle) inefficient since we only demand that the counter be able to represent a number corresponding to the number of bits of k. It may be that for other situations this is the wrong thing to assume.

一般情况如何?它看起来好像元编程是一种适当的方法。如何保持这个'sane'?也许正式地,需要一个编译时函数来映射(可能派生的)对类型的抽象类型需求。

What about the general case? It looks as though meta-programming is an appropriate approach. How to keep this 'sane'? Perhaps, formally, the requirement is for a compile-time function to map (potentially derived) abstract type requirements on to types.

也许这是YABL的工作Boost图书馆)!

Perhaps it's a job for YABL (Yet Another Boost Library)!

推荐答案

但实际上,如果你的T是128位,最高的移位操作数将是127,整洁地适合 char ...

But in reality, if your T would be 128 bit, the highest number of shift operations would be 127, neatly fitting a char...

那么,你不是过度设计循环变量类型吗? (可能是由于shift操作符i.s.o.平均增量,如litb指出)

So aren't you over-engineering the loop variable type a bit? (May be due to the shift operator i.s.o. plain increment, as pointed out by litb)

这篇关于C ++ - 通用编程 - 类型选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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