我有这样的代码,但我认为意图是明确的:
testmakeshared.cpp
#include <memory>
class A {
public:
static ::std::shared_ptr<A> create() {
return ::std::make_shared<A>();
}
protected:
A() {}
A(const A &) = delete;
const A &operator =(const A &) = delete;
};
::std::shared_ptr<A> foo()
{
return A::create();
}
但是当我编译它时,我得到了这个错误:
g++ -std=c++0x -march=native -mtune=native -O3 -Wall testmakeshared.cpp
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:52:0,
from /usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/memory:86,
from testmakeshared.cpp:1:
testmakeshared.cpp: In constructor ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc) [with _Tp = A, _Alloc = std::allocator<A>, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’:
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr_base.h:518:8: instantiated from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = A, _Alloc = std::allocator<A>, _Args = {}, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr_base.h:986:35: instantiated from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<A>, _Args = {}, _Tp = A, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:313:64: instantiated from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<A>, _Args = {}, _Tp = A]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:531:39: instantiated from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = A, _Alloc = std::allocator<A>, _Args = {}]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:547:42: instantiated from ‘std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = A, _Args = {}]’
testmakeshared.cpp:6:40: instantiated from here
testmakeshared.cpp:10:8: error: ‘A::A()’ is protected
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr_base.h:400:2: error: within this context
Compilation exited abnormally with code 1 at Tue Nov 15 07:32:58
这条消息基本上是在说模板实例化堆栈中::std::make_shared中的一些随机方法不能访问构造函数,因为它是受保护的。
但我真的想使用::std::make_shared和防止任何人创建这个类的对象不是由a::std::shared_ptr指向的。有什么办法可以做到吗?
当两个严格相关的类a和类B一起工作时,会出现一个更麻烦也更有趣的问题。
假设A是“主类”,B是“从类”。如果你想将B的实例化限制为A,你可以将B的构造函数设为private,并将B设为A的友例
class B
{
public:
// B your methods...
private:
B();
friend class A;
};
不幸的是,从a的方法调用std::make_shared<B>()将使编译器抱怨B::B()是私有的。
我对此的解决方案是在B内部创建一个公共Pass虚拟类(就像nullptr_t一样),它有私有构造函数,与a是朋友,并使B的构造函数为公共,并将Pass添加到它的参数中,就像这样。
class B
{
public:
class Pass
{
Pass() {}
friend class A;
};
B(Pass, int someArgument)
{
}
};
class A
{
public:
A()
{
// This is valid
auto ptr = std::make_shared<B>(B::Pass(), 42);
}
};
class C
{
public:
C()
{
// This is not
auto ptr = std::make_shared<B>(B::Pass(), 42);
}
};
基于CRTP的解决方案,允许对多个类进行分解,很容易启用,并适用于带参数的构造函数。它要求构造函数是受保护的(而不是私有的)。用法有点类似于enable_shared_from_this。它没有破坏受保护关键字的缺点,即使用::make_unique的类必须是友类。灵感来自Mark Tolley的回答。
实现:
template <typename ClassWithProtectedCtor>
class enable_protected_make_unique
{
protected: // important, if public then equivalent to having the constructor public which is what we want to avoid!
template <typename... Args>
static std::unique_ptr<ClassWithProtectedCtor> make_unique(Args &&... args)
{
class make_unique_enabler : public ClassWithProtectedCtor
{
public:
// it's from this line that comes the need to have the constructor protected, not private:
make_unique_enabler(Args &&... args) : ClassWithProtectedCtor(std::forward<Args>(args)...) {}
};
return std::make_unique<make_unique_enabler>(std::forward<Args>(args)...);
}
};
用法示例:
class Factory;
class MyClassWithProtectedCtor : public enable_protected_make_unique<MyClassWithProtectedCtor>
{
friend Factory;
private:
MyClassWithProtectedCtor(int a, double c) {};
}
class Factory
{
std::unique_ptr<MyClassWithProtectedCtor> CreateMyClassWithProtectedCtor(int a, double c)
{
return MyClassWithProtectedCtor::make_unique(a, c);
}
}
您可以用shared替换unique,或者在同一个“使能器”类中合并这两者。
免责声明:我没有在产品代码中测试,可能有缺点(例如当提到MyClassWithProtectedCtor类型时,会出现较长的错误消息)。
问题的根源在于,如果你加为好友的函数或类对你的构造函数进行低级调用,那么它们也必须加为好友。Std::make_shared并不是真正调用构造函数的函数,因此添加为好友并没有什么区别。
class A;
typedef std::shared_ptr<A> APtr;
class A
{
template<class T>
friend class std::_Ref_count_obj;
public:
APtr create()
{
return std::make_shared<A>();
}
private:
A()
{}
};
std::_Ref_count_obj实际上是在调用你的构造函数,所以它需要是一个友函数。因为这有点晦涩,所以我使用宏
#define SHARED_PTR_DECL(T) \
class T; \
typedef std::shared_ptr<T> ##T##Ptr;
#define FRIEND_STD_MAKE_SHARED \
template<class T> \
friend class std::_Ref_count_obj;
然后你的类声明看起来相当简单。如果你愿意,你可以创建一个宏来声明ptr和类。
SHARED_PTR_DECL(B);
class B
{
FRIEND_STD_MAKE_SHARED
public:
BPtr create()
{
return std::make_shared<B>();
}
private:
B()
{}
};
这实际上是一个很重要的问题。
为了使代码可维护、可移植,您需要隐藏尽可能多的实现。
typedef std::shared_ptr<A> APtr;
隐藏了你如何处理智能指针,你必须确保使用你的typedef。但是如果您总是必须使用make_shared来创建一个,这就违背了目的。
上面的示例强制使用类的代码使用智能指针构造函数,这意味着如果您切换到新的智能指针类型,您只需更改类声明,就有很大的机会完成任务。不要以为你的下一个老板或下一个项目会使用stl、boost等。
做了将近30年,我付出了巨大的时间代价、痛苦和副作用来修复多年前做错的事情。