C++ Logo

sg7

Advanced search

Re: CppMeta code reflection and C code reflection

From: Jonathan O'Connor <jonathan.oconnor_at_[hidden]>
Date: Fri, 23 Feb 2024 08:31:43 +0000
Robin,
I think all reflection proposals show how to implement enum to string.The CppMeta version seems more complicated than the P2996 proposal, but I think that was only because they wanted to show off lower level code generation capabilities.

Regards,
Jonathan


On Friday, 23 February 2024 at 04:39, Robin Rowe via SG7 <sg7_at_[hidden]> wrote:

> Yes, ok, I agree with you that my idea isn't suitable for the
> preprocessor. If in the compiler instead, what issues do you see?
>
> The use case I encounter most often when I wish I had reflection is
> converting enums into strings...
>
> enum Color { red, green, blue };
> const char* color_name[] = {"red","green","blue"};
> switch(n)
> case red:
> printf("Activated %s\n", color_name[red];
>
> I'd like a smaller solution than CppMeta C++ templates, to be able to
> generate color_name strings in C code without explicitly writing the
> color_name line of code. My issue with writing that line is if enum
> Color changes I may forget to update color_name strings to match.
>
> Ideas?
>
> Robin Rowe
> Beverly Hills, California
> *Chairman ISO WG21 SG14 C++ Banking and Financial Systems Subcommittee
>
> On 2/22/2024 4:13 PM, Jens Maurer wrote:
>
> > On 21/02/2024 01.19, Robin Rowe via SG7 wrote:
> >
> > > Thank you for posting the insightful thread about CppMeta as a potential
> > > implementation of C++ reflection. I'd like to ask about bringing code
> > > reflection to C.
> > >
> > > I program in both C++ and C. I am imagining, for code reflection in C, a
> > > new preprocessor operator ### (maybe called code-string) to go with #
> > > (stringize) and ## (concatenate).
> >
> > Please read [lex.phases] (in C++) or 5.1.1.2 (in C23).
> > The preprocessor runs way earlier than when C tokens are recognized as such.
> >
> > This won't work absent major surgery to foundational concepts of C
> > (and C++, if a compatible facility should be introduced there).
> >
> > Jens
> >
> > > However, unlike # and ##, using ###
> > > isn't limited to within a #define macro...
> > >
> > > // C preprocessor ### operator potential example:
> > >
> > > enum Color { red, green, blue };
> > >
> > > struct Point
> > > { int x;//some comment
> > > int y;
> > > int z;
> > > };
> > >
> > > void foo(int x);
> > >
> > > const char* s = ###(Color);
> > > puts(s);
> > > /* output:
> > > enum Color { red, green, blue };
> > > /
> > > s = ###(Color.2);
> > > puts(s);
> > > / output:
> > > enum blue = 2;
> > > /
> > > s = ###(Point);
> > > puts(s);
> > > / output:
> > > struct Point
> > > { int x;//some comment
> > > int y;
> > > int z;
> > > };
> > > /
> > > s = ###(Point.x);
> > > puts(s);
> > > / output:
> > > int x;
> > > /
> > > s = ###(Point.1);
> > > puts(s);
> > > / output (member #1):
> > > int y;
> > > /
> > > s = ###(Point.);
> > > puts(s);
> > > / output (how many members in struct):
> > > 3
> > > /
> > > s = ###(foo);
> > > puts(s);
> > > / output:
> > > void foo(int x);
> > > */
> > > s = ###(Hello); //compile-time error, Hello doesn't exist
> > >
> > > Code-string hasn't been proposed yet. Still at idea stage. CppMeta is
> > > much more sophisticated, seems too sophisticated for C. What are the
> > > most significant features of CppMeta missing in ### concept?
> > >
> > > Thoughts?
> > >
> > > Robin Rowe
> > > Beverly Hills, California
> > > *Chairman ISO WG21 SG14 C++ Banking and Financial Systems Subcommittee
>
> --
> SG7 mailing list
> SG7_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/sg7

Received on 2024-02-23 08:31:50