-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
augment libc++ test for smart pointers #18991
Conversation
zassert_equal(make_unique_data::ctors, 0, "ctor count not initialized"); | ||
zassert_equal(make_unique_data::dtors, 0, "dtor count not initialized"); | ||
auto d = std::make_unique<make_unique_data>(); | ||
zassert_true(static_cast<bool>(d), "allocation failed"); |
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.
Would it not be better to compare against nullptr
instead of casting to a bool?
Comment also applies to release check.
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.
No, the intent was specifically to take advantage of the defined behavior for operator bool()
on smart pointers (and other containers). Bare d
doesn't convert to a pointer because it's a smart pointer; you have to do d.get()
which is not what real code would use when checking whether the pointer was valid.
However that doesn't work for argument passing because it's not one of the cases where contextual conversion occurs, so here an explicit cast is required. It works for the release check because that code is a macro where the passed condition is !d
, where the conversion does occur.
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.
Ok missed the operator bool
in the c++ docs, note that operator ==
is overloaded so comparing to a nullptr
(or another unique pointer) is a valid operation.
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.
Heh; and I missed that, since I don't think I'd ever use a smart pointer that way. It'd happen in templates, though, so it's good it's there.
f3872cf
to
80314fa
Compare
This comment has been minimized.
This comment has been minimized.
Use of the test suite in C++ causes warnings because use of defined cast operators have the wrong target type. For example, many standard container APIs use operator bool() to test for empty containers. Code like zassert_true(v, "") fails to build when the test parameter is an int. Correct the argument type. This also causes any use of an assignment expression as a conditional in zassert to be diagnosed as a potential error. Signed-off-by: Peter A. Bigot <pab@pabigot.com>
… use Confirm that std::make_unique<> functions as intended. This is an indirect test of new/delete using best-practices API. Signed-off-by: Peter A. Bigot <pab@pabigot.com>
Confirms behavior of new and delete using best-practices API.
This also requires a change to the testsuite to use the correct type for the condition argument.