以下代码在VSC++2017中编译时没有任何错误,在gcc 7.3.0中无法编译 (
error: invalid static_cast from type 'int(int)' to type 'void*'
void* p = static_cast<void*>(func)
)。#include <iostream>
int func(int x) { return 2 * x; }
int main() {
void* p = static_cast<void*>(func);
return 0;
}
sizeof()
大小不同时。如果它们的大小相同,通常可以安全地相互转换为void*
。即便如此,虽然这种方法可能有效,但它并不具备可移植性,最好还是避免使用。 - doug