P
pedroalves
Hi all,
This is not a question about how to #define COMMA ,
Please keep reading.
Recently in binutils, we introduced a macro like this:
#define STRING_COMMA_LEN(STR) \
(STR), ((STR) ? sizeof (STR) - 1 : 0)
So in cases where we have a struct like this:
struct astruct
{
const char* str;
size_t len;
};
We can replace the manually inserted length:
struct astruct astruct_inst[] =
{ {"a string", 8} };
With:
struct astruct astruct_inst[] =
{ { STRING_COMMA_LEN("a string") } };
All good.
On with the question:
If you try to compile the code below, and strncmp happens to be a
macro,
the compiler will complain with an error, because the comma inserted
by STRING_COMMA_LEN will not be considered an operator by the time
strncmp is evaluated.
const char *a;
strncmp (a, STRING_COMMA_LEN("a string"));
Is there *any* way the STRING_COMMA_LEN could be changed
so this compiles, without touching the strncmp definition?
Or more generally, is there any way a macro can generate a comma
that will be considered an operator by the next macro?
Cheers,
Pedro Alves
This is not a question about how to #define COMMA ,
Please keep reading.
Recently in binutils, we introduced a macro like this:
#define STRING_COMMA_LEN(STR) \
(STR), ((STR) ? sizeof (STR) - 1 : 0)
So in cases where we have a struct like this:
struct astruct
{
const char* str;
size_t len;
};
We can replace the manually inserted length:
struct astruct astruct_inst[] =
{ {"a string", 8} };
With:
struct astruct astruct_inst[] =
{ { STRING_COMMA_LEN("a string") } };
All good.
On with the question:
If you try to compile the code below, and strncmp happens to be a
macro,
the compiler will complain with an error, because the comma inserted
by STRING_COMMA_LEN will not be considered an operator by the time
strncmp is evaluated.
const char *a;
strncmp (a, STRING_COMMA_LEN("a string"));
Is there *any* way the STRING_COMMA_LEN could be changed
so this compiles, without touching the strncmp definition?
Or more generally, is there any way a macro can generate a comma
that will be considered an operator by the next macro?
Cheers,
Pedro Alves