Conversation
added 4 commits
April 30, 2025 11:55
Testing revealed difficulties regarding extending json_encode with
user-defined types. In the following chain of calls:
struct S { ... };
json_encode(std::string, std::vector<std::array<S, 6>>); // 1
json_encode(std::ostream, std::vector<std::array<S, 6>>); // 2
json_encode(std::ostream, std::array<S, 6>*, ...); // 3
json_encode(std::ostream, std::array<S, 6>); // 4
json_encode(std::ostream, S*); // 5
json_encode(std::ostream, S); // 6, implemented in the user code
the compiler might only be able to find the user function if it was
called with unqualified names and argument-dependent lookup took
action. Also, without forward declarations provided by this patch the
compiler could not reach step 4 since json_encode for array has been
declared after json_encode for pointers. This suggests possible problems
with user and other STL containers. See
https://stackoverflow.com/questions/28187170/c-enforce-second-pass-name-lookup-in-template-function
https://akrzemi1.wordpress.com/2015/11/19/overload-resolution/
for the discussion.
One possible solution is to declare
template <typename T> void json_encode(std::ostream&, const T&);
and use template specializations instead of overloaded functions, but it
needs special casing to exclude arithmetic types implemented by a
different template --- and possibly it may clash in a similar way with
other user defined types.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Provide
json_encodeforchar*std::arrayImplement
json_encode_object--- a helper function to construct JSON objects out of variables and literals.There is a subtlety when it comes to extending
json_encodefor user-defined objects that I ran into while working on BUTTON high voltage control implementation, see 8361521. It needs further research. Please keep the commit message for the future.