ฉันมีโค้ด C ส่วนหนึ่งซึ่งฉันกำหนดโครงสร้างข้อมูลบางส่วนเป็นประเภทพื้นฐาน ฉันต้องการกำหนดประเภทพื้นฐานนี้ผ่านมาโครเพื่อให้สามารถเปลี่ยนแปลงได้ บางอย่าง la:
#define F_TYPE float // will use float as the fundamental type
// #define F_TYPE double // will use double instead
ด้วยเหตุนี้ ฉันจึงต้องเลือกฟังก์ชันที่เหมาะสมเล็กน้อยโดยขึ้นอยู่กับประเภท ตัวอย่างเช่น สำหรับ float
ฉันควรใช้ fabsf
จาก <cmath>
แต่สำหรับ double
ฉันควรใช้ fabs
บางสิ่งบางอย่าง la:
#if (F_TYPE == float)
#define F_TYPE_ABS(x) fabsf(x)
#elif (F_TYPE == double)
#define F_TYPE_ABS(x) fabs(x)
#else
#error "need to specify math functions to use with type F_TYPE"
#endif
แต่ดูเหมือนว่าจะไม่ทำงาน: คอมไพเลอร์ของฉันให้คำเตือนประเภทนี้:
In file included from tests_vec3.cpp:2:0:
../src/kiss_clang_3d.h:27:16: warning: "double" is not defined, evaluates to 0 [-Wundef]
#define F_TYPE double
^
../src/kiss_clang_3d.h:32:6: note: in expansion of macro ‘F_TYPE’
#if (F_TYPE == float)
^~~~~~
../src/kiss_clang_3d.h:32:16: warning: "float" is not defined, evaluates to 0 [-Wundef]
#if (F_TYPE == float)
^~~~~
มีความคิดใดที่ฉันจะสร้างงานแบบนี้ได้อย่างไร? ฉันน่าจะพลาดบางอย่างเกี่ยวกับวิธีใช้มาโคร
_Generic
ไม่ทำ ไม่ทำงานเหรอ? - person H Walters   schedule 24.04.2021