Rob*_*ahy 11 c++ gcc mingw c++11 gcc-pedantic
显然,在函数指针和对象指针之间进行转换是一般意义上的未定义行为,但POSIX(请参阅:dlsym)和WinAPI(请参阅:GetProcAddress)需要这样做.
鉴于此,并且鉴于这样的代码无论如何都是针对特定于平台的API,它对于函数指针和对象指针不兼容的平台的可移植性实际上是无关紧要的.
但是--Wableantic无论如何警告它,并且#pragma GCC diagnostic ignored "-Wpedantic"没有效果:
warning: ISO C++ forbids casting between pointer-to-function and pointer-to-object [enabled by default]
Run Code Online (Sandbox Code Playgroud)
我希望保持-Wpedantic启用,因为它确实提供了良好的警告,但我不希望在关于对象指针强制转换的函数指针的无关警告中出现真正的警告和错误.
有没有办法实现这个目标?
在Windows上运行GCC 4.8.0(MinGW):
gcc (rubenvb-4.8.0) 4.8.0
Run Code Online (Sandbox Code Playgroud)
代码示例
#include <windows.h>
#include <iostream>
int main (void) {
std::cout << *reinterpret_cast<int *>(GetProcAddress(LoadLibraryA("test.dll"),"five")) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
发射(使用-Wpedantic):
warning_demo.cpp: In function 'int main()':
warning_demo.cpp:7:87: warning: ISO C++ forbids casting between pointer-to-funct
ion and pointer-to-object [enabled by default]
std::cout << *reinterpret_cast<int *>(GetProcAddress(LoadLibraryA("test.dll"),
"five")) << std::endl;
^
Run Code Online (Sandbox Code Playgroud)
我认为你可以在这里使用 g++ 的system_header指令:
包装_GetProcAddress.h:
#ifndef wrap_GetProcAddress_included
#define wrap_GetProcAddress_included
#pragma GCC system_header
template <typename Result>
Result GetProcAddressAs( [normal parameters] )
{
return reinterpret_cast<Result>(GetProcAddressAs( [normal parameters] ));
}
#endif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2683 次 |
| 最近记录: |