Skip to content

Is it really necessary to [[nodiscard]] the return value of the write() method? #1104

Answered by stephenberry
scalpel4k asked this question in Q&A
Discussion options

You must be logged in to vote

There are a few ways to clean up the code when you don't care about successful writing:

  • Use std::ignore
std::ignore = glz::write_json(value, buffer);

This is probably what you want, albeit it requires a bit more code from the developer.

  • Use the exceptions API.
#include "glaze/glaze_exceptions.hpp"

glz::ex::write_json(value, buffer);

Now, in the unlikely case of an error you'll get an exception, but you don't have to handle it here.

  • Cast to void
(void)glz::write_json(value, buffer);

I would prefer std::ignore because it is more obvious what is going on, but this is an alternative approach.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@scalpel4k
Comment options

@stephenberry
Comment options

@scalpel4k
Comment options

Answer selected by stephenberry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants