我有这样的代码,但我认为意图是明确的:
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指向的。有什么办法可以做到吗?
因为我不喜欢已经提供的答案,所以我决定继续搜索,并找到了一个解决方案,它不像之前的答案那么通用,但我更喜欢它(tm)。回想起来,它并不比Omnifarius提供的好多少,但可能也有其他人喜欢它:)
这不是我发明的,而是Jonathan Wakely (GCC开发人员)的想法。
不幸的是,它并不适用于所有的编译器,因为它依赖于std::allocate_shared实现中的一个小变化。但是这个变化现在是针对标准库的建议更新,所以将来可能会得到所有编译器的支持。它适用于GCC 4.7。
c++标准库工作组变更请求如下:
http://lwg.github.com/issues/lwg-active.html#2070
GCC补丁的用法示例如下:
http://old.nabble.com/Re%3A--v3--Implement-pointer_traits-and-allocator_traits-p31723738.html
解决方案的思想是使用std::allocate_shared(而不是std::make_shared)和一个自定义分配器,该分配器被声明为具有私有构造函数的类的友元。
OP的示例如下所示:
#include <memory>
template<typename Private>
struct MyAlloc : std::allocator<Private>
{
void construct(void* p) { ::new(p) Private(); }
};
class A {
public:
static ::std::shared_ptr<A> create() {
return ::std::allocate_shared<A>(MyAlloc<A>());
}
protected:
A() {}
A(const A &) = delete;
const A &operator =(const A &) = delete;
friend struct MyAlloc<A>;
};
int main() {
auto p = A::create();
return 0;
}
一个基于我正在使用的实用程序的更复杂的示例。在这种情况下,我不能使用卢克的解决方案。但Omnifarius的作品可以改编。在前面的例子中,每个人都可以使用MyAlloc创建A对象,但在这个例子中,除了create()方法之外,没有其他方法可以创建A或B对象。
#include <memory>
template<typename T>
class safe_enable_shared_from_this : public std::enable_shared_from_this<T>
{
public:
template<typename... _Args>
static ::std::shared_ptr<T> create(_Args&&... p_args) {
return ::std::allocate_shared<T>(Alloc(), std::forward<_Args>(p_args)...);
}
protected:
struct Alloc : std::allocator<T>
{
template<typename _Up, typename... _Args>
void construct(_Up* __p, _Args&&... __args)
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
};
safe_enable_shared_from_this(const safe_enable_shared_from_this&) = delete;
safe_enable_shared_from_this& operator=(const safe_enable_shared_from_this&) = delete;
};
class A : public safe_enable_shared_from_this<A> {
private:
A() {}
friend struct safe_enable_shared_from_this<A>::Alloc;
};
class B : public safe_enable_shared_from_this<B> {
private:
B(int v) {}
friend struct safe_enable_shared_from_this<B>::Alloc;
};
int main() {
auto a = A::create();
auto b = B::create(5);
return 0;
}
[编辑]我阅读了上面提到的标准化std::shared_ptr_access<>提案的线程。其中有一个响应,指出了对std::allocate_shared<>的修复以及它的使用示例。我已经将其调整为下面的工厂模板,并在gcc c++ 11/14/17下测试了它。它与std::enable_shared_from_this<>一起工作,所以显然比我在这个答案中的原始解决方案更可取。在这儿……
#include <iostream>
#include <memory>
class Factory final {
public:
template<typename T, typename... A>
static std::shared_ptr<T> make_shared(A&&... args) {
return std::allocate_shared<T>(Alloc<T>(), std::forward<A>(args)...);
}
private:
template<typename T>
struct Alloc : std::allocator<T> {
template<typename U, typename... A>
void construct(U* ptr, A&&... args) {
new(ptr) U(std::forward<A>(args)...);
}
template<typename U>
void destroy(U* ptr) {
ptr->~U();
}
};
};
class X final : public std::enable_shared_from_this<X> {
friend class Factory;
private:
X() { std::cout << "X() addr=" << this << "\n"; }
X(int i) { std::cout << "X(int) addr=" << this << " i=" << i << "\n"; }
~X() { std::cout << "~X()\n"; }
};
int main() {
auto p1 = Factory::make_shared<X>(42);
auto p2 = p1->shared_from_this();
std::cout << "p1=" << p1 << "\n"
<< "p2=" << p2 << "\n"
<< "count=" << p1.use_count() << "\n";
}
[Orig]我发现了一个解决方案使用共享指针别名构造函数。它允许ctor和dtor都是私有的,以及final说明符的使用。
#include <iostream>
#include <memory>
class Factory final {
public:
template<typename T, typename... A>
static std::shared_ptr<T> make_shared(A&&... args) {
auto ptr = std::make_shared<Type<T>>(std::forward<A>(args)...);
return std::shared_ptr<T>(ptr, &ptr->type);
}
private:
template<typename T>
struct Type final {
template<typename... A>
Type(A&&... args) : type(std::forward<A>(args)...) { std::cout << "Type(...) addr=" << this << "\n"; }
~Type() { std::cout << "~Type()\n"; }
T type;
};
};
class X final {
friend struct Factory::Type<X>; // factory access
private:
X() { std::cout << "X() addr=" << this << "\n"; }
X(int i) { std::cout << "X(...) addr=" << this << " i=" << i << "\n"; }
~X() { std::cout << "~X()\n"; }
};
int main() {
auto ptr1 = Factory::make_shared<X>();
auto ptr2 = Factory::make_shared<X>(42);
}
注意,上面的方法不适用于std::enable_shared_from_this<>,因为初始std::shared_ptr<>是针对包装器的,而不是针对类型本身的。我们可以用一个与工厂兼容的等价类来解决这个问题……
#include <iostream>
#include <memory>
template<typename T>
class EnableShared {
friend class Factory; // factory access
public:
std::shared_ptr<T> shared_from_this() { return weak.lock(); }
protected:
EnableShared() = default;
virtual ~EnableShared() = default;
EnableShared<T>& operator=(const EnableShared<T>&) { return *this; } // no slicing
private:
std::weak_ptr<T> weak;
};
class Factory final {
public:
template<typename T, typename... A>
static std::shared_ptr<T> make_shared(A&&... args) {
auto ptr = std::make_shared<Type<T>>(std::forward<A>(args)...);
auto alt = std::shared_ptr<T>(ptr, &ptr->type);
assign(std::is_base_of<EnableShared<T>, T>(), alt);
return alt;
}
private:
template<typename T>
struct Type final {
template<typename... A>
Type(A&&... args) : type(std::forward<A>(args)...) { std::cout << "Type(...) addr=" << this << "\n"; }
~Type() { std::cout << "~Type()\n"; }
T type;
};
template<typename T>
static void assign(std::true_type, const std::shared_ptr<T>& ptr) {
ptr->weak = ptr;
}
template<typename T>
static void assign(std::false_type, const std::shared_ptr<T>&) {}
};
class X final : public EnableShared<X> {
friend struct Factory::Type<X>; // factory access
private:
X() { std::cout << "X() addr=" << this << "\n"; }
X(int i) { std::cout << "X(...) addr=" << this << " i=" << i << "\n"; }
~X() { std::cout << "~X()\n"; }
};
int main() {
auto ptr1 = Factory::make_shared<X>();
auto ptr2 = ptr1->shared_from_this();
std::cout << "ptr1=" << ptr1.get() << "\nptr2=" << ptr2.get() << "\n";
}
最后,有人说clang抱怨Factory::Type在作为朋友使用时是私有的,所以如果是这种情况,就把它设为公共。暴露它没有坏处。