[英]How do I transform a large, often-used C++ macro with trivial per-use changes into a C++ template or similar?
[英]How to create DLLAPI() template mixin in the D programming language that behaves similar to the well-known DLLAPI (or similar) macro in C/C++?
正如大多數人所知,在C / C ++中,當我處理動態庫時,我會編寫一個與此類似的宏。
#ifdef _WIN32
# define DLLAPI __declspec(dllimport)
#else
# define DLLAPI
#endif
我想要做的是D編程語言中的模板mixin,所以我可以很好地聲明某個庫中的函數:
mixin DLLAPI(int, "function1", string, int); // int function1(const char* arg1, int arg2)
mixin DLLAPI(byte, "function2"); // char function2()
// etc
第一個問題是 - 為此使用模板mixin是一個好主意,還是只在version (Windows) {} version (linux) {} //...
塊中編寫代碼? 第二個問題 - 如何在函數名稱之后很好地獲取所有類型? :)
解決方案只是使用extern(System)而不是extern(C)/ extern(Windows) - 它默認為Windows中的stdcall和Linux中的cdecl和whanot。
從而:
extern(System) {
int function(...) foo;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.