gpt4 book ai didi

c++ - 函数指针位移

转载 作者:太空宇宙 更新时间:2023-11-04 16:14:42 24 4
gpt4 key购买 nike

我有以下代码:

void (* point)();
point=prova;
unsigned long int imm8 = point<<24;

...
void prova(){
...
}

第三行代码我有错误:

invalid operands to binary << (have 'void (*)()' and 'int')

我试图将移位运算符应用于函数指针,但出现以下错误。我该怎么办?

最佳答案

我不明白你为什么要这样做,但错误是正确的,5.8 部分的 C++ 标准草案 Shift operators 说;

The operands shall be of integral or unscoped enumeration type and integral promotions are performed.[...]

您可以使用 reinterpret_cast如果需要,将其转换为整数类型(uintptr_t)。链接引用包含以下示例:

int i = 7;

// pointer to integer and back
uintptr_t v1 = reinterpret_cast<uintptr_t>(&i); // static_cast is an error

警告

这种类型的转换只是有条件地支持,我们可以从 C++ 草案标准部分 5.2.10 Reinterpret cast 中看到这一点,其中说:

Converting a function pointer to an object pointer type or vice versa is conditionally-supported. The meaning of such a conversion is implementation-defined, except that if an implementation supports conversions in both directions, converting a prvalue of one type to the other type and back, possibly with different cv- qualification, shall yield the original pointer value.

关于c++ - 函数指针位移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24018770/

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