Remove explicitly forwarded constructors in rtc::FinalRefCountedObject

those constructors may expose protected constructors that are not suppose to be public
using T::T forwards constructors with respect to protected access specifier

Bug: None
Change-Id: I3fcce0fd88bbdf538bc85baab833397f787408d8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/216240
Reviewed-by: Tommi <tommi@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33840}
This commit is contained in:
Danil Chapovalov 2021-04-26 16:32:27 +02:00 committed by Commit Bot
parent dfc7d99f99
commit 80b7628a55
2 changed files with 20 additions and 11 deletions

View File

@ -64,21 +64,14 @@ template <class T>
class FinalRefCountedObject final : public T {
public:
using T::T;
// Until c++17 compilers are allowed not to inherit the default constructor,
// and msvc doesn't. Thus the default constructor is forwarded explicitly.
// Until c++17 compilers are allowed not to inherit the default constructors.
// Thus the default constructors are forwarded explicitly.
FinalRefCountedObject() = default;
explicit FinalRefCountedObject(const T& other) : T(other) {}
explicit FinalRefCountedObject(T&& other) : T(std::move(other)) {}
FinalRefCountedObject(const FinalRefCountedObject&) = delete;
FinalRefCountedObject& operator=(const FinalRefCountedObject&) = delete;
template <class P0>
explicit FinalRefCountedObject(P0&& p0) : T(std::forward<P0>(p0)) {}
template <class P0, class P1, class... Args>
FinalRefCountedObject(P0&& p0, P1&& p1, Args&&... args)
: T(std::forward<P0>(p0),
std::forward<P1>(p1),
std::forward<Args>(args)...) {}
void AddRef() const { ref_count_.IncRef(); }
void Release() const {
if (ref_count_.DecRef() == RefCountReleaseStatus::kDroppedLastRef) {

View File

@ -125,6 +125,22 @@ TEST(FinalRefCountedObject, CanWrapIntoScopedRefptr) {
EXPECT_TRUE(ref2->HasOneRef());
}
TEST(FinalRefCountedObject, CanCreateFromMovedType) {
class MoveOnly {
public:
MoveOnly(int a) : a_(a) {}
MoveOnly(MoveOnly&&) = default;
int a() { return a_; }
private:
int a_;
};
MoveOnly foo(5);
auto ref = make_ref_counted<MoveOnly>(std::move(foo));
EXPECT_EQ(ref->a(), 5);
}
// This test is mostly a compile-time test for scoped_refptr compatibility.
TEST(RefCounted, SmartPointers) {
// Sanity compile-time tests. FooItf is virtual, Foo is not, FooItf inherits