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

Shadowed Member in merge_patch #1339

Closed
ax3l opened this issue Nov 7, 2018 · 2 comments
Closed

Shadowed Member in merge_patch #1339

ax3l opened this issue Nov 7, 2018 · 2 comments

Comments

@ax3l
Copy link
Contributor

ax3l commented Nov 7, 2018

  • What is the issue you have?

Compiling in downstream projects with -Wshadow warns on void merge_patch(const basic_json& patch) shadowing a member of this.

https://github.com/nlohmann/json/blob/master/include/nlohmann/json.hpp#L7866

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

Compile with Clang 5.0.0 and CXXFLAGS="-Wshadow".

  • What is the expected behavior?

Better not to shadow a member of this :)

  • And what is the actual behavior instead?

Throws a warning, e.g.: https://travis-ci.org/openPMD/openPMD-api/jobs/449795263

single_include/nlohmann/json.hpp:
In member function ‘void nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>::merge_patch(const nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer>&)’:
single_include/nlohmann/json.hpp:20126:5: error: declaration of ‘patch’ shadows a member of 'this' [-Werror=shadow]
     {

Clang 5.0.0 on Travis CI (Ubuntu 14.04)

  • Did you use a released version of the library or the version from the develop branch?

We use release 3.4.0.

N/A

first seen by @franzpoeschel

@nlohmann
Copy link
Owner

nlohmann commented Nov 8, 2018

Thanks for reporting. I do not like such warnings, because in the end they make the code "uglier", because I think it is fine to both have a member function called patch and a function parameter patch. But I think one can fix this by changing the parameter name to p or something...

@ax3l
Copy link
Contributor Author

ax3l commented Nov 8, 2018

Thanks for your feedback! Yes, I know that this "uglifies" the code a little by e.g. introducing consequent prefixes for members or the like in most cases... But I also got bitten quite a bit by shadowed vars in the past in large projects which are hard to catch sometimes.

If you are ok with that, I would really just rename the parameter name: #1346

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

No branches or pull requests

2 participants