boostorg / boostorg/parser

parsing into private fields

Open
#182 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
182
Forks
28
PR merge metrics
No merged PRs in 30d

Description

Currently my biggest gripe with Spirit.X3 is that you can't parse into a class with private fields. This leaves you with two options:

1. use a proxy type, e.g.

```cpp

class Myclass_impl {
public:
char c;
};

class Myclass : private Myclass_impl {
public:
Myclass_impl &base() { return *this; }
};

bp::rule rule{"rule"};
const auto rule_def = bp::char_;
BOOST_PARSER_DEFINE_RULES(rule);

void func() {
std::string str{"c"};
Myclass result;

bp::parse(str, rule, result.base());
}

```

2. Just don't use private members, which means your parser is no longer a parser, just a validator.

`1.` makes public headers unreadable as users would have to look into implementation headers to see the actual class body, while
`2.` violates the [parse, don't validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/) principle and thus allows for invalidly constructed objects that would never be returned from a successful parse.

Are there any plans to remedy this, e.g. via a Boost.Parser helper class that the user class could befriend, or via a specialization that grants access to the private members (Boost.Describe comes to mind) ?

Or am I just missing a workaround that everyone has been using for the past decade

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.