-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Define all C++ model constructors explicit #2944
Define all C++ model constructors explicit #2944
Conversation
With explicit specifier we prevent of using copy initialization: no more of
|
Codecov Report
@@ Coverage Diff @@
## master #2944 +/- ##
=======================================
Coverage 73.41% 73.41%
=======================================
Files 99 99
Lines 8801 8801
Branches 1389 1389
=======================================
Hits 6461 6461
Misses 1915 1915
Partials 425 425 Continue to review full report at Codecov.
|
@vfdev-5 we could still do:
But as you said this will no longer be not allowed:
More specifically what we don't allow the compiler to do is silently take the integer 100, pass it on the constructor as first argument to create a temporary object and then copy to x (thought optimizations can happen here by compiler). I think it's better to not allow this syntax because it leads to less readable code and potential bugs. Hence it's by default a warning on clang tidy. :) |
@datumbox I totally agree ! Definition like that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot Vasilis!
* Making all model constructors explicit. * formatting.
* Making all model constructors explicit. * formatting.
Static analysis on the C++ code at
csrc/models/
produces the following warning:Clang-Tidy: Constructors that are callable with a single argument must be marked explicit to avoid unintentional implicit conversions
By allowing implicit calls the following misleading usage is possible:
This can be stopped by defining all the constructors that receive up to 1 mandatory parameter as explicit:
After the change, the following 2 initializations are valid:
Note that thought this might be considered theoretically a breaking change on the C++ API, I believe not many users use the old initialization as it's quite misleading.
I tested the changes by manually running the test_cpp_models.py unit-tests. All pass except of
test_mnasnet0_5
,test_mnasnet0_75
andtest_mnasnet1_3
which also fail on master.