Skip to content

Add experimental C++26 reflection ORM#2545

Draft
1ukidev wants to merge 2 commits into
drogonframework:masterfrom
1ukidev:feature/refl
Draft

Add experimental C++26 reflection ORM#2545
1ukidev wants to merge 2 commits into
drogonframework:masterfrom
1ukidev:feature/refl

Conversation

@1ukidev

@1ukidev 1ukidev commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

This PR aims to implement a new way of using the ORM (orm::ReflectiveMapper) through the compile-time reflection introduced in C++26. I have been working on this feature for personal use for some time, and I thought it would be interesting to share it here. I am not sure how open the project is to adopting newer C++ standards.

Thanks to reflection annotations, it is currently possible to write code such as:

namespace refl = drogon::orm::reflection;

struct [[=refl::Table{"users"}]] User
{
    [[=refl::PrimaryKey{}, =refl::AutoGenerated{}]]
    std::optional<std::uint64_t> id;

    [[=refl::Column{"user_name"}]]
    std::string name;

    std::optional<std::string> email;
}

All metadata is generated statically at compile time, including the query strings used for retrieval, insertion, counting, updating, and deletion. This practically eliminates the need for utilities such as drogon_ctl.

The workflow with orm::ReflectiveMapper is as simple as:

const auto client = DbClient::newSqlite3Client("filename=:memory:", 1);

ReflectiveMapper<User> mapper{client};

User user;
user.name = "Ada";
user.email = "ada@example.test";

mapper.insert(user);

const auto nameColumn = mapper.columnName<^^User::name>();
auto loaded = mapper.findOne(
    Criteria{nameColumn, CompareOperator::EQ, std::string{"Ada"}});

A example is available in examples/orm_reflection/main.cc. For now, only synchronous and callback-based database communication is supported, but other approaches can be discussed in the future.

Currently, only GCC 16 provides official experimental support for reflection, which may require additional caution when using it in production. I'm marking this as a draft for now. Feel free to ask if you have any questions.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant