【发布时间】:2016-12-15 01:52:43
【问题描述】:
我想知道如何在 python 中使用与 template 类似的代码,因为它在 C++ 代码示例中使用:
template <typename T>
unsigned int counting_bit(unsigned int v){
v = v - ((v >> 1) & (T)~(T)0/3); // temp
v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); // temp
v = (v + (v >> 4)) & (T)~(T)0/255*15; // temp
return v;
}
如何在 python 中以与 C++ 中提到的相同的方式对具有变量 typename 的对象进行类型转换?
【问题讨论】:
-
到目前为止你的python代码是什么?
-
即使使用 C++,是否需要
template来计算位数?有一个n & (n - 1)使用循环计数位的技巧。 -
@VaibhavBajaj 我还没有编写 python 代码,因为我仍在试图弄清楚如何对对象进行类型转换。
-
你要在 python 中传递什么
T? python默认只有一种整数类型,你要不要用ctypes? -
@Holt 我会通过 ctypes,我也觉得 DeepSpace 的回答很有帮助。
标签: python c++ templates function-templates