模板自身友元的问题(模仿BOOST的shared_ptr时碰到)?
以下是<boost\shared_ptr.hpp>中的部分代码:
template<class T> class shared_ptr
{
... ...(省略)
// Tasteless as this may seem, making all members public allows member templates
// to work in the absence of member template friends. (Matthew Langston)
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
private:
template<class Y> friend class shared_ptr;
template<class Y> friend class weak_ptr;
#endif
}; // shared_ptr
(VC6.0成功编译通过)
而我写的时候template<class Y> friend class shared_ptr;却在那行报错。
出现
error C2059: syntax error : '<end Parse>'
see reference to class template instantiation 'shared_ptr<T>' being compiled
error C2143: syntax error : missing ';' before '}'
see reference to class template instantiation 'shared_ptr<T>' being compiled
error C2238: unexpected token(s) preceding ';'
see reference to class template instantiation 'shared_ptr<T>' being compiled
... ...
去掉template<class Y> friend class shared_ptr;正常。但是我要派生类的智能指针模板内部可以访问其父类的私有成员。就是shared_ptr<inherit_class>内部访问shared_ptr<parent_class>的私有成员(不用get,就用friend class)。
但是<boost\shared_ptr.hpp>可以,我写的却编译不过.那位大侠99.