首页
\
问答
\
使用Boost.Python将boost :: gregorian :: date暴露给Python(Exposing boost::gregorian::date to Python using Boost.Python)
使用Boost.Python将boost :: gregorian :: date暴露给Python(Exposing boost::gregorian::date to Python using Boost.Python)
我想使用Boost.Python为Python提供boost :: gregorian :: date。 但是当Boost日期类中没有一个函数时,如何创建一个像样的
__str__
函数呢? 我想这样写:BOOST_PYTHON_MODULE(mymodule) { class_<boost::gregorian::date>("Date") .add_property("year", &boost::gregorian::date::year) .add_property("month", &boost::gregorian::date::month) .def("__str__", ???) ; }
I'd like to make boost::gregorian::date available to Python using Boost.Python. But how do I create a decent
__str__
function when one is not available on the Boost date class? I'd like to write it like this:BOOST_PYTHON_MODULE(mymodule) { class_<boost::gregorian::date>("Date") .add_property("year", &boost::gregorian::date::year) .add_property("month", &boost::gregorian::date::month) .def("__str__", ???) ; }
原文:https://stackoverflow.com/questions/17068111
更新时间:2021-09-14 13:09
最满意答案
怎么样的:
template <typename T> T foo(unsigned short length) { return (T(1) << (length * 4)) - 1; }
How about something like:
template <typename T> T foo(unsigned short length) { return (T(1) << (length * 4)) - 1; }
相关问答
更多-
我编译你的第一个例子,在g++进行优化,并生成与你的第三个解决方案相同的代码。 事实上,用一个小小的bitset(320位)完全展开它。 没有调用函数来确保a和b的内容在main是未知的,它实际上已经优化了整个东西(知道两者都是0)。 课程:编写明显的可读代码并让编译器处理它。 I compiled your first example with optimization in g++ and it produced code identical to your third solution. In fac ...
-
对于&运算符(以及大多数其他运算符),在计算运算之前,任何小于int操作数都将被提升为int 。 从C99标准(6.5.10 - 描述按位AND运算符): 通常的算术转换是在操作数上执行的。 (6.3.1.8 - 描述通常的算术转换): 在两个操作数上执行整数提升 (6.3.1.1 - 描述整数提升): 如果int可以表示原始类型的所有值,则该值将转换为int ; 否则,它将转换为unsigned int 。 这些被称为整数促销。 For the & operator (and most other ope ...
-
您需要确定您感兴趣的不同部分的位范围。查看列表,主要类型有2个十六进制数字,子类型有4个数字。 使用AND( & )将它们分开。 major = type & 0xff0000; subtype = type & 0x00ffff; You need to determine the bit ranges of the different pieces you're interested in. Looking at the list, there are 2 hex digits devoted to ...
-
是的,gcc会优化它,因为它是一个完全不变的表达式。 要检查这个汇编代码,例如使用这个工具https://gcc.godbolt.org/ #include
#define SOME_CONSTANT 0x111UL #define SOME_OFFSET 2 #define SOME_MASK 7 #define SOME_VALUE ((SOME_CONSTANT) << (SOME_OFFSET)) & (SOME_MASK) void foo() { pri ... -
在C中,算法至少在int的大小下执行。 这意味着, ~0xFC将返回一个int 。 甚至更多,这个值是0xFF03 ,超出了char的范围(有符号或不是)。 因此,作业会给出两个警告。 你可以试试 signed char sc = ~0xFC & 0xFF; 看看编译器是否知道结果是在char范围内(gcc在添加& 0xFF后不会抱怨)。 你也可以尝试添加一个明确的演员: signed char sc = (signed char)~0xFC; // also possible: // signed ch ...
-
C ++中的快速按位问题(Fast Bitwise Question in C++)[2023-11-06]
怎么样的: templateT foo(unsigned short length) { return (T(1) << (length * 4)) - 1; } How about something like: template T foo(unsigned short length) { return (T(1) << (length * 4)) - 1; } -
C:按位不在某位(C: Bitwise NOT on certain bit)[2021-09-24]
与要翻转的位进行XOR。 int c = 0x10; // 10000b int m = 0x08; // 01000b c ^= m; // 11000b XOR with the bits that you want to flip. int c = 0x10; // 10000b int m = 0x08; // 01000b c ^= m; // 11000b -
C ++按位AND对于单个位(C++ bitwise AND for single bits)[2023-02-10]
最好的方法是将表达式转换为布尔值。 这提高了可读性: bool result; result = 0 != (a & 8); 与0的比较可能是不必要的,因为编译的代码将在变量b写入true或false 。 但这避免了一个警告: warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning) 如果您不喜欢Yoda演讲,则可能会颠倒顺序或比较参数。 这种风格可以帮助避免C编程语言中的意外分配。 The ... -
C ++中结构的按位异或(Bitwise XOR on structs in C++)[2022-05-24]
重载结构中的成员: struct X { X& operator ^= (const X& other) { //... return *this; } }; Overload the member in the struct: struct X { X& operator ^= (const X& other) { //... return *this; } }; -
来自维基百科: 按位AND运算符是单个&符号:&。 它只是AND的一种表示,它对操作数的位而不是操作数的真值进行处理。 按位二进制AND以二进制形式对数字的每个位置中的位进行逻辑AND(如上表所示)。 在您的示例110010和1 , 1被认为是000001 ,然后每个位都是有效的,您将得到结果。 事实上,我使用这种方法: 1&number来检查偶数和奇数。 这是如何: if(1 & num) printf("it is odd"); else printf("it is even"); 这是它的 ...