Skip to content

Aliases with reflection^^ #99

Closed
Closed

Description

@vaijns

With GCC on trunk now having C++26 reflection implemented I wanted to give it a try and thought an easy and useful use-case for it would be getting rid of those SQLPP_CREATE_NAME_TAG(name) macros in sqlpp... Or at least having an alternative.

So with trial and error I got something working that might be usable for this, see this godbolt link.

Please note, I must admit I haven't read the whole proposal yet :D and my knowledge on how to ideally use reflection is limited. So it is very likely that there are better and simpler ways to do this. It was more about getting something working in general right now.

But what something like this should allow (haven't tried it in sqlpp itself yet), is creating aliases inline like some_sqlpp_function().as(make_reflection_alias<"my_alias">()) or with an overload of as even some_sqlpp_function().as<"my_alias">().

With feature-test macros it should be possible to only include it for compilers and C++ standard versions having it implemented.

If you'd be interested in anything like this, I'd be happy to integrate it into the project and open a PR. Also, feel free to critique if you'd like it to be different in any way.

-Jonas

Activity

  1. rbock commented on Feb 12, 2026

    @rbock
    Owner

    Hi Jonas,

    Thanks for reaching out with this. Using reflection is the natural next step. I was thinking about creating an experimental sqlpp26 already. Conditionally adding a few things here is also an option, of course. Happy to take a look at a PR.

    I did some experiments, too, using the bloomberg fork of clang: https://github.com/bloomberg/clang-p2996

    Here is a snapshot: table.cpp

    As you can see, I was also thinking of the something.as<"name">() variant, which is my favorite at this point :-)

    Thanks,
    Roland

  2. vaijns commented on Feb 12, 2026

    @vaijns
    ContributorAuthor

    I did some experiments, too, using the bloomberg fork of clang: https://github.com/bloomberg/clang-p2996
    Here is a snapshot: table.cpp
    As you can see, I was also thinking of the something.as<"name">() variant, which is my favorite at this point :-)

    Oh, interesting, I do see similarities in what you tried. I wanted to try the Bloomberg implementation as well a few months ago but didn't want to bother further after I couldn't get the standard library linked right away. Probably just some small thing but gcc made it a bit easier I think. C:

    -Jonas

  3. MeanSquaredError commented on Feb 13, 2026

    @MeanSquaredError
    Contributor

    With GCC on trunk now having C++26 reflection implemented I wanted to give it a try and thought an easy and useful use-case for it would be getting rid of those SQLPP_CREATE_NAME_TAG(name) macros in sqlpp... Or at least having an alternative.

    I wonder if that will make it easier to add automatically alias data members for the various sql functions in the result data types. sqlpp11 was adding very nicely data members called "exists", "count", "avg", etc. and unfortunately this is gone from sqlpp23. It would be nice to have that back if it wouldn't complicate the codebase too much.

    On a side note: it is funny that cppreference still doesn't even list reflection (P2996) as a feature of C++26
    https://en.cppreference.com/w/cpp/26.html

  4. vaijns commented on Feb 13, 2026

    @vaijns
    ContributorAuthor

    I think functions you still have to add "manually" similarly to operator() in my example. Unless I'm missing something, idk. But I'd guess you could do it in a somewhat readable way.
    Though changing result types would either mean annoying ifdefs (because here it's not some isolated code where you can just wrap the whole thing in an ifdef but instead it's used "everywhere"), or breakimg change requiring a compiler with reflection.

    Yeah, cppreference is pretty much frozen atm as it's maintainer wanted to update the software but hasn't found the time to fully do so afaik.
    In terms of compiler support, there's https://cppstat.dev now.

  5. rbock commented on Feb 13, 2026

    @rbock
    Owner

    @MeanSquaredError The auto-aliases for some functions create a surprising amount of mental overload in the library code, that's why they are gone in sqlpp23.

    max(tab.id).as<"maxid">() will feel quite natural, and that is where I want this to get to. Actually, my hope is that we will be able to write max(tab.id).as("maxid") one day.

    @vaijns I think that conditionally adding an overload for .as<"something"> would not be super hard if the generated code mimics the current code using SQLPP_CREATE_NAME_TAG.

  6. MeanSquaredError commented on Feb 13, 2026

    @MeanSquaredError
    Contributor

    The auto-aliases for some functions create a surprising amount of mental overload in the library code, that's why they are gone in sqlpp23.

    max(tab.id).as<"maxid">()will feel quite natural, and that is where I want this to get to. Actually, my hope is that we will be able to writemax(tab.id).as("maxid")` one day.

    Well, I guess it could be considered an improvement over the current aliasing system indeed.

    In terms of compiler support, there's https://cppstat.dev now.

    @vaijns Thanks a lot for the link! I find this site quite helpful, given that cppreference doesn't update now.

  7. vaijns commented on Feb 13, 2026

    @vaijns
    ContributorAuthor

    @vaijns I think that conditionally adding an overload for .as<"something"> would not be super hard if the generated code mimics the current code using SQLPP_CREATE_NAME_TAG.

    I agree, creating an alias is pretty isolated, which is why this was the first thing I wanted to try. I'm gonna try to get it working in sqlpp and I'll let you know once I've got something.

  8. vaijns commented on Feb 13, 2026

    @vaijns
    ContributorAuthor

    @rbock got a first working example, you can have a look already if you want but I'll definitely still add those tests I mentioned I forgot in the PR. Probably some other things to do still as well. Let me know. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions