boostorg / boostorg/pfr

Expected to work with Lambdas?

Open
#137 3 comments 0 reactions 0 assignees View on GitHub
compiler-or-standard-limitation
Dominant language
C++
Stars
1.5k
Forks
176
Avg merge
5d 21h
Merged PRs (30d)
1

Description

Hi, I see in the Limitations section

> Boost.PFR library works with types that satisfy the requirements of SimpleAggregate: aggregate types without base classes, const fields, references, or C arrays:

A casual reader may have seen on the internet "Lambdas are just syntax sugar around a struct with `operator()`", and expect that Boost.PFR would be able to introspect the types of the capture list of a lambda, since they are "just structs".

Sadly, this does not appear to work in Boost.PFR.

```
#include

void function()
{
auto lambda = [](void)mutable{};
using lambda_t = decltype(lambda);
static_assert(0 == boost::pfr::tuple_size_v);
}
```

This gives the error:

```
D:\builds\cm-systest\system\pub\dists\boost\boost/pfr/detail/fields_count.hpp(285): error C2338: static_assert failed: '====================> Boost.PFR: Type must be aggregate initializable.'
D:\builds\cm-systest\system\pub\dists\boost\boost/pfr/tuple_size.hpp(33): note: see reference to function template instantiation 'size_t boost::pfr::detail::fields_count(void) noexcept' being compiled
with
[
T=lambda_t
]
```

on the visual studio 2022 compiler, with similar errors on clang 16. (Both in C++17 mode, if that makes a difference).

I think it might be helpful for people searching for libraries to do introspection if you clearly document that lambdas are or are not supported in the Limitations section, with a brief explanation of why not (e.g. not aggregate types, because compiler-magic).

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.