public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Passing c blocks to pre processor
@ 2022-05-28  5:48 Yair Lenga
  2022-05-28  7:40 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Yair Lenga @ 2022-05-28  5:48 UTC (permalink / raw)
  To: gcc

Hi,

I am trying to define macros that will accept code blocks. This will be used to create high level structures (like iterations, etc.). Ideally, I would like to do something like (this is simplified example, actual code much more application specific):

Foreach(var, low, high, block)

While should translate to:
For (int var=low ; var < high ; var ++) block 

The challnge is the gcc pre processor does not accept blocks. It assume a comma terminate a block. For example:
Foreach(foo, 1, 30, { int z=5, y=3, …}) will invoke the macro with 5 arguments: (1) foo (2) 1, (3) 30, (4) { int z=5}, (5) y=3,

Is there a way to tell CPP that an argument that start with ‘{‘ should extend until the matching ‘}’ ? Similar to the way ‘(‘ in macro arguments will extend till matching ‘)’.

Thanks, yair.


Sent from my iPad

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Passing c blocks to pre processor
  2022-05-28  5:48 Passing c blocks to pre processor Yair Lenga
@ 2022-05-28  7:40 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2022-05-28  7:40 UTC (permalink / raw)
  To: Yair Lenga; +Cc: gcc

On Sat, 28 May 2022, 06:49 Yair Lenga via Gcc, <gcc@gcc.gnu.org> wrote:

> Hi,
>
> I am trying to define macros that will accept code blocks. This will be
> used to create high level structures (like iterations, etc.). Ideally, I
> would like to do something like (this is simplified example, actual code
> much more application specific):
>
> Foreach(var, low, high, block)
>
> While should translate to:
> For (int var=low ; var < high ; var ++) block
>
> The challnge is the gcc pre processor does not accept blocks. It assume a
> comma terminate a block. For example:
> Foreach(foo, 1, 30, { int z=5, y=3, …}) will invoke the macro with 5
> arguments: (1) foo (2) 1, (3) 30, (4) { int z=5}, (5) y=3,
>
> Is there a way to tell CPP that an argument that start with ‘{‘ should
> extend until the matching ‘}’ ? Similar to the way ‘(‘ in macro arguments
> will extend till matching ‘)’.
>


This question seems off-topic on this mailing list, as it's not about GCC
development. It would be more suited to the gcc-help list.

I don't think you can do that. You need to use __VA_ARGS__ to accept
arbitrary code containing commas. You could look at how the
Boost.Preprocessor library works.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-28  7:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-28  5:48 Passing c blocks to pre processor Yair Lenga
2022-05-28  7:40 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).