473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to pass COMMA as an operator into macro. (Not the FAQ).

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

Sep 18 '06 #1
6 2584
pedroalves wrote:
Recently in binutils, we introduced a macro like this:

#define STRING_COMMA_LEN(STR) \
(STR), ((STR) ? sizeof (STR) - 1 : 0)
Why use the conditional operator, you will get 0 for "" anyway?
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?
One way is to force a function call...

(strncmp)(a, STRING_COMMA_LEN("a string"));

But I think stylistically, it's better to scrap your macro and make the
code more obvious...

#define LITLEN(x) ((sizeof x) - 1)

static char a_string[] = "a string"; /* or #define, but somewhere
more
accessible that buried deep in the source code */

strncmp(a, a_string, LITLEN(a_string));
Or more generally, is there any way a macro can generate a comma
that will be considered an operator by the next macro?
Not in the way you want, AFAIK.

You can use an intermediate macro...

#define STRNCMPLIT(a,b) strncmp(a, b)

STRNCMPLIT(a, STRING_COMMA_LEN("a string"));

Or...

#define STRNCMPLIT2(a,b) \
APPLY(foo, a, STRING_COMMA_LEN(b))

#define APPLY(m, a, b) m(a, b)

STRNCMPLIT2(a, "a_string");

But the fact is... strncmp takes three arguments. All you're doing is
obfuscating
that. The situation is no different for the general case.

--
Peter

Sep 18 '06 #2
Peter Nilsson wrote:
pedroalves wrote:
Recently in binutils, we introduced a macro like this:

#define STRING_COMMA_LEN(STR) \
(STR), ((STR) ? sizeof (STR) - 1 : 0)

Why use the conditional operator, you will get 0 for "" anyway?
So that you will get 0 for NULL.

Sep 18 '06 #3
Ark
pedroalves wrote:
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
If I remember correctly the macro expansion rules in this late hour, you
need to wrap the /target/ macro to achieve the result you want:
#define STRNCMP(str, combo) strncmp(str, combo)
- Ark
Sep 18 '06 #4

Peter Nilsson wrote:
pedroalves wrote:
Is there *any* way the STRING_COMMA_LEN could be changed
so this compiles, without touching the strncmp definition?

One way is to force a function call...

(strncmp)(a, STRING_COMMA_LEN("a string"));
Humm, I can't get this to compile.
#define my_strncmp(A, B, C)

#define STRING_COMMA_LEN(STR) \
(STR), ((STR) ? sizeof (STR) - 1 : 0)

int main()
{
const char* a;
(my_strncmp)(a, STRING_COMMA_LEN("a string"));
return 0;
}

main.c:9: error: `my_strncmp' undeclared (first use in this function)

I must be missing something.
>
But the fact is... strncmp takes three arguments. All you're doing is
obfuscating
that. The situation is no different for the general case.
Yes, I agree. This was just an example.
I wanted this to build new macros on top of existing ones.
Thanks all, this has been very enlightning.

Sep 18 '06 #5
pedroalves wrote:
Peter Nilsson wrote:
pedroalves wrote:
Is there *any* way the STRING_COMMA_LEN could be changed
so this compiles, without touching the strncmp definition?
One way is to force a function call...

(strncmp)(a, STRING_COMMA_LEN("a string"));

Humm, I can't get this to compile.
#define my_strncmp(A, B, C)

#define STRING_COMMA_LEN(STR) \
(STR), ((STR) ? sizeof (STR) - 1 : 0)

int main()
{
const char* a;
(my_strncmp)(a, STRING_COMMA_LEN("a string"));
return 0;
}

main.c:9: error: `my_strncmp' undeclared (first use in this function)

I must be missing something.
That's because you haven't declared a (non-macro) function called
my_strncmp.

If you're now telling me that your 'strncmp' example wasn't strncmp but
was
infact a different macro foo (say) that calls a function not called
foo, then
that changes things.

--
Peter

Sep 18 '06 #6
Harald van Dijk wrote:
Peter Nilsson wrote:
pedroalves wrote:
Recently in binutils, we introduced a macro like this:
>
#define STRING_COMMA_LEN(STR) \
(STR), ((STR) ? sizeof (STR) - 1 : 0)
Why use the conditional operator, you will get 0 for "" anyway?

So that you will get 0 for NULL.
I figured, although that use is incompatible with the strncmp example
given
later in the OP's post.

--
Peter

Sep 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
8
by: mrstephengross | last post by:
I'm using gcc 3.3.1 to compile the following code (below). I've written a macro to simplify writing operators. The macro uses the '##' operator to paste together 'operator' and the name of the...
11
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
2
by: s.subbarayan | last post by:
Dear all, What does this following piece of code do? #define CONVERT_SELF_PTR(type,in,out) \ (((in) != NULL) ? ((out) = (type *)(in), RETURN_OK) : ERROR_PARAM) especially can someone let...
2
by: grid | last post by:
Hi, I need some clarifications on how the comma operator is used to return values from function like macros.I saw a typical implementation as : #define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 ,...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
17
by: Frederick Gotham | last post by:
I know there's a sequence point at a comma, e.g.: int main(void) { int a = 1; a++, ++a, a *= 3, a <<= 4; /* Perfectly okay */ }
15
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.