Repository navigation
Description
In the docs on the README, the Reflective programming section suggests in the second example that rfl::to_view() can be used to iterate over the fields at compile-time, with a relevant example using view.apply(...)...
However, I am unable to use it in a constexpr method forced to execute at compile-time, here is a contrived demo that reproduces the issue. It fails to compile because it complains that rfl::to_view() cannot be used in a constant expression:
#include <rfl.hpp>
struct Pod {
std::uint16_t foo; // 2
std::uint32_t bar; // 4
float baz; // 4
};
template <typename Struct>
constexpr std::size_t get_packed_size() {
std::size_t size = 0;
Struct unused{};
const auto view = rfl::to_view(unused);
view.apply([&](const auto& field) {
size += sizeof(decltype(*field.value()));
});
return size;
}
int main() {
static_assert(
get_packed_size<Pod>() ==
sizeof(std::uint16_t) + sizeof(std::uint32_t) + sizeof(float)
);
}
As a counter-example, if one instead defines the function as non-constexpr, and uses assert() instead of static_assert(), then the program both compiles and runs successfully without any assertion failures:
#include <cassert>
#include <rfl.hpp>
struct Pod {
std::uint16_t foo; // 2
std::uint32_t bar; // 4
float baz; // 4
};
template <typename Struct>
std::size_t get_packed_size() {
std::size_t size = 0;
Struct unused{};
const auto view = rfl::to_view(unused);
view.apply([&](const auto& field) {
size += sizeof(decltype(*field.value()));
});
return size;
}
int main() {
assert(
get_packed_size<Pod>() ==
sizeof(std::uint16_t) + sizeof(std::uint32_t) + sizeof(float)
);
}By the way, it would be nice if reflect-cpp provided a nicer way than this for me to iterate over all the fields of an object and get their types. The default-constructed variable unused is only provided here because I couldn't work out how else to ask reflect-cpp to iterate the fields of a struct for me without providing it with an object of that struct's type first. For my purposes, I only care about their types, not their values, in this context...
I suspect the issue I am experiencing here might be highly related to that mentioned in #220, which if I'm not mistaken, also seems to rest on the inability for rfl::to_view() to be used in a constant expression. I peeked at your function declarations and I noted that rfl::to_view() and other functions it calls are not declared constexpr, that might be required since I don't think constexpr can be deduced in general?
Activity
saxbophone commentedon Aug 16, 2026
Oh, it occurs to me that you probably also want the error message from the compiler. This is what I get from Clang on macOS:
I've also seen the same issue occur with MSVC on Windows. I'm not able to provide an error log for that, but it was complaining about essentially the same issue (
rfl::to_view()cannot be used in a constant expression).saxbophone commentedon Aug 16, 2026
I did a bit of further digging and hacking on reflect-cpp. The underlying issue seems to be that a lot of the library's own types (NamedTuple, Tuple, etc...) aren't
LiteralTypeswhich precludes their use in template arguments and certain constexpr-contexts, in particular:constexpr.I've tried hacking on reflect-cpp to make these changes, and I've hit a road-block at this particular method in
Tuple.hpp. I present here the modified version withXXXcomments added by me, documenting what I've tried...Note the
std::construct_at()version, the last one I tried, fails with the following log messages:Details
liuzicheng1987 commentedon Aug 17, 2026
@saxbophone what we mean by "at compile time" is that the fields are resolved at compile time, not that the function itself is applied at compile time.
The difficulty is that the tuple implementation uses placement new, which makes it difficult to make this fully constexpr.
But I will take a look at what can be done for this particular use case.
saxbophone commentedon Aug 17, 2026
I think I understand. Perhaps the docs could be clarified as the current wording is a bit confusing as to this nuance.
Yeah, I looked into replacing it with
std::construct_at(), but I couldn't get the casting of the first pointer argument to work. I was only rushing it, perhaps it can be done with a bit more care and time...Much appreciated. I assume that at some point, reflect-cpp internally has access to the types of all the fields in the struct, as like a template parameter-pack or something similar (perhaps a variadic tuple)? If this could be exposed somehow in a way that could be accessed by client code, that would probably be enough for my needs...
liuzicheng1987 commentedon Aug 19, 2026
@saxbophone C++ 26 offers a much simpler way to implement a non-recursive tuple. There isn't a lot of compiler support for it yet, but it might be the future.