aybe
aybe

Reputation: 16662

Generate a member name using preprocessor directives?

I would like to achieve the following but I can't get it right:

struct SamplerState
{
    int i;
};

#define SAMPLER_TYPE Point
#define SAMPLER_MODE_U ClampU
#define SAMPLER_MODE_V ClampV
#define SAMPLER_NAME( a, b, c ) ( ##a ##_ ##b ##_ ##c)
#define SAMPLER SamplerState SAMPLER_NAME(SAMPLER_TYPE, SAMPLER_MODE_U, SAMPLER_MODE_V)

int main()
{
    SAMPLER = {};
}

I expect the name of SAMPLER to be Point_ClampU_ClampV but it isn't when debugging, it is SAMPLER_TYPE_SAMPLER_MODE_U_SAMPLER_MODE_V instead:

enter image description here

Question:

How can I achieve that, if possible at all?

Upvotes: 1

Views: 111

Answers (1)

KamilCuk
KamilCuk

Reputation: 141698

  1. You can't join ( with a in (##a. No need to tho.
  2. You have to have another level of expansion, another macro, to let a, b and c expand.

struct SamplerState
{
    int i;
};


#define SAMPLER_TYPE Point
#define SAMPLER_MODE_U ClampU
#define SAMPLER_MODE_V ClampV
#define SAMPLER_NAME_IN(a, b, c)  ( a##_ ##b##_##c )
#define SAMPLER_NAME(a, b, c)  SAMPLER_NAME_IN(a, b, c)
#define SAMPLER SamplerState SAMPLER_NAME(SAMPLER_TYPE, SAMPLER_MODE_U, SAMPLER_MODE_V)

int main()
{
    SAMPLER = {};
}

Upvotes: 3

Related Questions