Careful about function-like macro parameter naming
Kim Lillås
1 min read
I needed to create a function-like macro which set a parameter in a default settings struct, simple enough.
struct Settings
{
unsigned enabled;
};
struct Settings defaultSettings;
#define SetEnabled(enabled) (defaultSettings.enabled = enabled)
This compiles fine, and even works in some cases:
// This works fine
bool enabled = true;
SetEnabled(enabled);
I ran into trouble when I renamed the local parameter:
bool newEnabledSetting = true;
SetEnabled(newEnabledSetting);
// Compilation error:
// error: 'struct Settings' has no member named 'newEnabledSetting'
After a bit of head scratching I rechecked the #define and realized the function-like macro parameter and struct parameter shared the same name (enabled).
The #define would look something like this if I send in a parameter named "newEnabledSetting" to it:
bool newEnabledSetting = true;
SetEnabled(newEnabledSetting);
// Is equivalent to
defaultSettings.newEnabledSetting = newEnabledSetting
The lesson for today: make damn sure your function-like macro parameters do not share names with anything you are working with.
#define SetEnabled(macroParamEnabled) (defaultSettings.enabled = macroParamEnabled)
0
Subscribe to my newsletter
Read articles from Kim Lillås directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by