부울에게 캐스팅에 과부하가 걸리는 내 자신의 ptr로 부작용 ptr을 감싸려고했습니다. 또한, 필자의 ptr에서 일부 부스트 침입 ptrmethod에 대한 액세스를 제한하고 싶습니다. 처음에는 ptr의 private 멤버로 관입 ptr을 강화하고 bool 관용구를 사용했습니다. bool 관용구를 사용하여 잘 작동했습니다. 내 자신의 특별한 이유 때문에 새로운 ptr이 침입 형 ptr 유형이되어야하므로 개인 상속을 사용했지만 작동하지 않았습니다. 어떤 이유로 gcc는 내 ptr을 부작용 ptr로 처리하려고 시도하고 공개적으로 노출 된 bool 관용구를 사용하기를 거부합니다. 여기 는 보여주는 샘플 코드입니다 그것은 :GCC는 개인 상속을 부모로 변환합니다.
부스트 방해 PTR 개인적으로 상속하는 경우g++ -std=c++11 ./test.cpp
...
./test.cpp: In function 'int main()':
./test.cpp:88:16: error: 'boost::intrusive_ptr<Object>' is an inaccessible base of 'PtrB<Object>'
if (obj == NULL)
^
In file included from /usr/include/boost/smart_ptr/intrusive_ptr.hpp:167:0,
from /usr/include/boost/intrusive_ptr.hpp:16,
from ./test.cpp:1:
/usr/include/boost/smart_ptr/detail/operator_bool.hpp:60:10: error: 'bool boost::intrusive_ptr<T>::operator!() const [with T = Object]' is inaccessible
bool operator!() const BOOST_NOEXCEPT
^
./test.cpp:92:10: error: within this context
if (!obj)
^
./test.cpp:92:10: error: 'boost::intrusive_ptr<Object>' is not an accessible base of 'PtrB<Object>'
, 무엇을 수행하는 GCC 그것을 사용하려고 : 컴파일 오류 코드 결과에 PtrB를 사용
#include <boost/intrusive_ptr.hpp>
#include <stdio.h>
class Object
{
public:
Object() : _refCount(0) {}
void addRef() { _refCount++; }
unsigned int releaseRef() { return --_refCount; }
private:
unsigned int _refCount;
};
inline void intrusive_ptr_release(Object* p)
{
if (p->releaseRef() == 0)
{
delete p;
}
}
inline void intrusive_ptr_add_ref(Object* p)
{
p->addRef();
}
// Option A: using private member
template<class ObjectT>
class PtrA
{
public:
explicit PtrA(ObjectT* obj) : _obj(obj) {}
PtrA() {}
ObjectT* operator->() { return _obj.operator->(); }
const ObjectT* operator->() const { return _obj.operator->(); }
ObjectT* get() { return _obj.get(); }
const ObjectT* get() const { return _obj.get(); }
typedef const ObjectT* (PtrA::*unspecified_bool_type)() const;
operator unspecified_bool_type() const
{
if (_obj != NULL)
{
printf("ptr is not null\n");
}
return _obj.get() == 0 ? 0: (unspecified_bool_type)&PtrA::get;
}
private:
boost::intrusive_ptr<ObjectT> _obj;
};
// Option B: using private inheritance
template<class ObjectT>
class PtrB : private boost::intrusive_ptr<ObjectT>
{
public:
explicit PtrB(ObjectT* obj) : boost::intrusive_ptr<ObjectT>(obj) {}
PtrB() {}
using boost::intrusive_ptr<ObjectT>::operator->;
using boost::intrusive_ptr<ObjectT>::get;
typedef const ObjectT* (PtrB::*unspecified_bool_type)() const;
operator unspecified_bool_type() const
{
const ObjectT* p = boost::intrusive_ptr<ObjectT>::get();
if (p != NULL)
{
printf("ptr is not null\n");
}
return p == 0 ? 0 : (unspecified_bool_type)&PtrB::get;
}
};
int main()
{
// this verison compiles
// PtrA<Object> obj(new Object());
PtrB<Object> obj(new Object());
if (obj == NULL)
{
printf("object is null\n");
}
if (!obj)
{
printf("object is null\n");
}
return 0;
}
대신 공개 bool 관용구? 내가 이름 검색의 규칙을 변경하지 않습니다 GCC에게 4.8.5
@WhiZTiM OP의 예제를 수정했습니다. 주석 처리 된 행을 변경하면됩니다. – Barry
@ 배리, 오 ... 장님 ... 그래. .. 재현. 대답에 대한 설명이 나와 있습니다. :-) – WhiZTiM