Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding value_from support without including a Boost.JSON header #549

Closed
pdimov opened this issue Apr 27, 2021 · 5 comments · Fixed by #610
Closed

Adding value_from support without including a Boost.JSON header #549

pdimov opened this issue Apr 27, 2021 · 5 comments · Fixed by #610
Assignees
Labels

Comments

@pdimov
Copy link
Member

pdimov commented Apr 27, 2021

It's important for class authors to be able to write the appropriate tag_invoke overload for their types without needing to include a Boost.JSON header (in order to not require Boost.JSON to be present when not used.) A minimal example is https://godbolt.org/z/jqMj4o4j4. The immediate problem is that value_from can't be forward declared because it has a default argument.

@vinniefalco
Copy link
Member

These declarations should have comments explaining the rationale for their signatures

@pdimov
Copy link
Member Author

pdimov commented Apr 27, 2021

The easiest fix here is to change

template<class T> value value_from( T&& t, storage_ptr sp = {} );

to

template<class T> value value_from( T&& t );
template<class T> value value_from( T&& t, storage_ptr sp );

@pdimov
Copy link
Member Author

pdimov commented Apr 27, 2021

... however, doing so still doesn't enable the code to compile, because value_from returns value by value, and value is incomplete, so the API as-is can't be used with only forward declarations.

So instead of changing the above declaration, we need to introduce another API that works when value is incomplete:

template<class T> void value_from_( value& v, T&& t );

with some better name.

The tag_invoke overload then becomes

template<class W> void tag_invoke( boost::json::value_from_tag const&, W& w, X const& x )
{
    auto& obj = w.emplace_object();
    value_from_( obj[ "m" ], x.m );
}

which is even easier to use and automatically uses the right storage. (And the signature matches that of tag_invoke.)

@vinniefalco
Copy link
Member

we need to introduce another API

Is this a public API?

@pdimov
Copy link
Member Author

pdimov commented Apr 27, 2021

How would anyone use it otherwise?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants