glaze icon indicating copy to clipboard operation
glaze copied to clipboard

[Question] Possible to support types like `std::inplace_vector`?

Open lord-ne opened this issue 11 months ago • 3 comments

Hello. I was wondering, how does the library interact with container types like std::inplace_vector (or its beman implementation) that have a known maximum capacity? Is it possible for a user to define custom formatting for these types so that glaze will return an error code if attempting to read a JSON array into them that is too large (instead of throwing std::bad_alloc)? I'm not seeing a way for a user to do that except by completely redoing all of the code parsing JSON arrays in their own from method, but I'm happy to be shown otherwise.

lord-ne avatar Apr 02 '25 01:04 lord-ne

Nice, I hadn't realized std::inplace_vector was accepted to C++26. This is great!

A bit of work would be needed to return a nice error code if attempting to read out of the array bounds. But, it shouldn't be too difficult.

Do you have any additional thoughts of what a C++20 concept for std::inplace_vector would look like to differentiate it from std::vector and similar interfaces? I think we should be able to use the fact that it takes a size_t as its second template parameter. And, capacity() should be able to distinguish it from a std::span and the like.

I'll keep this issue alive until support is added. I'll probably use the beman implementation for testing. Thanks for pointing that out.

stephenberry avatar Apr 02 '25 14:04 stephenberry

I think a try_emplace_backable concept makes sense: For classes that have a try_emplace_back() method, use that instead of emplace_back, and return an error code if it fails.

My other idea would be to return an error if we want to add an element but the size of the container is already max_size() (which is a method that all standard containers support). But I like the first idea better.

lord-ne avatar Apr 02 '25 14:04 lord-ne

Nice. I think checking for try_emplace_back is a good solution. I probably won't get to this right away, so feel free to submit a pull request if you need support sooner, otherwise I'll get this done when I'm able.

stephenberry avatar Apr 02 '25 14:04 stephenberry