是否可以在宏中進行無效性檢查和訪問?C99結構體成員訪問的宏擴展
如:
#define LOG(mystruct, severity, format, ...) ({ \
severity_t current = ERROR; \
if (mystruct) { \
current = mystruct->error_level; \
} \
if (severity >= current) { \
... //handle logging
} \
})
如果我把這個與LOG(NULL, DEBUG, "test %s", "one");
我得到一個錯誤這樣:
error: member reference base type 'void' is not a structure or union note: expanded from macro 'LOG' current = mystruct->error_level;
MYSTRUCT被定義爲:
typedef struct mystruct_t {
severity_t error_level;
}
我想允許使用NULL的可能性。例如:創建結構本身時發生錯誤的情況。
NULL-> error_level'不可編譯。也許你可以使用'MYSTRUCT * ptr =(severity);'然後'if if(ptr)current = ptr-> error_level;' –
內聯函數會更整齊 –
讓我稍微說明一下結構。 'mystruct_t'是一個超結構,它包含'severity_t'。我想允許爲'mystruct'處理NULL的可能性(例如:在創建'mystruct_t'本身時發生錯誤時) – bge0