問題のコード:
template<typename T>
class SharedObject {
public:
typedef boost::intrusive_ptr<T> Pointer;
typedef boost::intrusive_ptr<T const> ConstPointer;
inline Pointer GetPointer() {
return Pointer(this); //Ambiguous call here
}
inline ConstPointer GetPointer() const {
return ConstPointer(this);
}
...
次のように使用します。
template <typename T>
class SomeClass: public SharedObject<SomeClass<T> > {
public:
static inline boost::intrusive_ptr<SomeClass<T> > Create() {
return (new SomeClass)->GetPointer();
}
};
int main()
{
auto v = SomeClass<int>::Create();
}
GCC (4.4.1) でブースト 1.41 を使用すると、GetPointer() の最初の (非 const) バージョンを開始すると、次のエラーが発生します。
error: call of overloaded ‘intrusive_ptr SharedObject<SomeClass<int> >* const)’ is ambiguous
boost/smart_ptr/intrusive_ptr.hpp:118: note: candidates are: boost::intrusive_ptr<T>::intrusive_ptr(boost::intrusive_ptr<T>&&) [with T = SomeClass<int>] <near match>
boost/smart_ptr/intrusive_ptr.hpp:94: note: boost::intrusive_ptr<T>::intrusive_ptr(const boost::intrusive_ptr<T>&) [with T = SomeClass<int>] <near match>
boost/smart_ptr/intrusive_ptr.hpp:70: note: boost::intrusive_ptr<T>::intrusive_ptr(T*, bool) [with T = SomeClass<int>] <near match>
私の C++ の難解なスキルには、あいまいさがある理由がまったくわかりません。188 行目と 94 行目の 2 つの候補は、既存の intrusive_ptr 右辺値参照を使用していますが、これSharedObject::this
は確かにそうではありません。ただし、最終候補は完全に一致します (bool 引数はオプションです)。
問題が何であるかについて私に教えてくれる人はいますか?
EDIT +回答:私はついにそれを実現しました
inline Pointer GetPointer() {
return Pointer(this); //Ambiguous call here
}
this
Pointer typedef が SomeClass である間、SharedObject を参照します。(これは、Butterworth がすぐに指摘したことのほとんどです)。
inline Pointer GetPointer() {
return Pointer(static_cast<C*>(this));
}
私はthis
本当に SomeClass であり、SharedObject から継承していることを知っているので、static_cast はテンプレート クラスを一巡させます。