open-source-parsers / open-source-parsers/jsoncpp

Make Json::Value(std::string) etc... explicit

Open
#1,137 8 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug enhancement Language Conformance
Dominant language
C++
Stars
8.9k
Forks
2.7k
Avg merge
31m
Merged PRs (30d)
1

Description

Describe the bug
As it stands, we cannot write code like:

error_code write(std::ostream& in, const string_view s) noexcept;  // 1
error_code write(std::ostream& in, const Json::Value& o) noexcept;

Because the call to '1' this is ambiguous:

write(in, "foo"s);

The problem is that C++ detects that it can automagically convert "foo"s to either a string_view OR a Json::Value.

It's an annoying reality that explicit isn't the default for constructors. It should be. Be that as it may, Jsoncpp would play nicer if it did use explicit.

To Reproduce
Steps to reproduce the behavior:

  1. See code snippets above.

Expected behavior
A clear and concise description of what you expected to happen.

An error message like:

serialize.cpp:343:12: error: call to 'write' is ambiguous
      ec = write(in, str(o));
           ^~~~~
serialize.cpp:260:12: note: candidate function
error_code write(std::ostream& out, std::string_view x) noexcept
           ^
serialize.cpp:339:12: note: candidate function
error_code write(std::ostream& in, const Json::Value& o) noexcept

Desktop (please complete the following information):

Affects all version compiling under a conformant c++ compiler.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Json::Value constructors and reproduce the overloaded write call shown in the issue with a std::string literal. Make the relevant string-related constructors explicit, then verify that the sample no longer produces an ambiguous-call error and add regression coverage if the project’s existing tests provide an appropriate entry point.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.