gpt4 book ai didi

c++ - 在 MAKEWORD/MAKEWPARAM 中使用 bool 值而不是 BOOL 值

转载 作者:行者123 更新时间:2023-11-27 22:37:13 26 4
gpt4 key购买 nike

// Rebuild the "Assign Flags"
m_dwStudentAssignFlags = MAKEWPARAM(
MAKEWORD(bIncluded[INDEX(StudentAssign::kItem1)] ? TRUE : FALSE, bIncluded[INDEX(StudentAssign::kItem2)] ? TRUE : FALSE),
MAKEWORD(bIncluded[INDEX(StudentAssign::kItem3)] ? TRUE : FALSE, bIncluded[INDEX(StudentAssign::kItem4)] ? TRUE : FALSE));

bIncluded 是一个包含 4 个 boolean 类型元素的固定数组。

上面的代码有效,但我想知道是否有任何具体原因导致我无法将代码简化为:

// Rebuild the "Assign Flags"
m_dwStudentAssignFlags = MAKEWPARAM(
MAKEWORD(bIncluded[INDEX(StudentAssign::kItem1)], bIncluded[INDEX(StudentAssign::kItem2)]),
MAKEWORD(bIncluded[INDEX(StudentAssign::kItem3)], bIncluded[INDEX(StudentAssign::kItem4)]));

最佳答案

MAKEWPARAM(a, b) 在底层使用 C-casts,类似于

(unsigned long)(a)

(unsigned __int64)(a)

bool 转换为 unsigned longunsigned __int64 是完全有效的,并且 C++ 标准(4.7/4,[conv.integral] ) 保证转换 falsetrue 将产生 01:

If the source type is bool, the value false is converted to zero and the value true is converted to one.

FALSETRUE#define 用于 01。因此,您的简化是有效的并且等同于原始代码。

关于c++ - 在 MAKEWORD/MAKEWPARAM 中使用 bool 值而不是 BOOL 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52749298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com