Skip to content

Commit

Permalink
Use std::is_base_of to assert WeakPtr is used correctly
Browse files Browse the repository at this point in the history
SupportsWeakPtr::StaticAsWeakPtr used to use std::is_convertible to
ensure the argument is derived fromSupportsWeakPtrBase.
However, the assertion fails also when the class has multiple
SupportWeakPtrBases as its ancestor, and in this case the error message
from static_assert is inaccurate.
Also, it doesn't match to base_nocompile_tests expectation.

This CL replaces the std::is_convertible with std::is_base_of and
updates base_nocompile_tests expectation.

Review URL: https://codereview.chromium.org/1805503002

Cr-Commit-Position: refs/heads/master@{#381415}
  • Loading branch information
tzik authored and Commit bot committed Mar 16, 2016
1 parent 4ccd1db commit 8df3dd0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 3 additions & 4 deletions base/memory/weak_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,9 @@ class SupportsWeakPtrBase {
// function that makes calling this easier.
template<typename Derived>
static WeakPtr<Derived> StaticAsWeakPtr(Derived* t) {
using convertible =
std::is_convertible<Derived*, internal::SupportsWeakPtrBase*>;
static_assert(convertible::value,
"AsWeakPtr argument must inherit from SupportsWeakPtr");
static_assert(
std::is_base_of<internal::SupportsWeakPtrBase, Derived>::value,
"AsWeakPtr argument must inherit from SupportsWeakPtr");
return AsWeakPtrImpl<Derived>(t, *t);
}

Expand Down
2 changes: 1 addition & 1 deletion base/memory/weak_ptr_unittest.nc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void WontCompile() {
WeakPtr<Unrelated> ptr = AsWeakPtr(&f);
}

#elif defined(NCTEST_AMBIGUOUS_ANCESTORS) // [r"fatal error: ambiguous conversion from derived class 'base::MultiplyDerivedProducer' to base class 'base::internal::SupportsWeakPtrBase':"]
#elif defined(NCTEST_AMBIGUOUS_ANCESTORS) // [r"fatal error: member 'AsWeakPtr' found in multiple base classes of different types"]

void WontCompile() {
MultiplyDerivedProducer f;
Expand Down

0 comments on commit 8df3dd0

Please sign in to comment.