Re: gcc specialization of template membert of template class
spamsink42@gmail.com wrote:
i have a template class which stores a pointer which may need to be
recast as requested by client code. here's the relevant subset of the
code:
template <class PointerClass>
class StorePointer {
public:
template <class RequestedPointerClass>
boost::shared_ptr<RequestedPointerClass> getPointer() const {
return
boost::dynamic_pointer_cast<RequestedPointerClass>(storedPointer_);
}
private:
boost::shared_ptr<PointerClass> storedPointer_;
};
sometimes the class required by the client is exactly that stored in
which case i want to forego the cast.
How is 'RequestedPointerClass' template argument is deduced? I think
it's not deduced (no context to deduce it from), so you have to put
an explicit template argument there, right, like
myStorePointer.getPointer<OtherPointer>()
right? So, I suggest overloading. See below.
Visual Studio 2005 lets me do
that with the following specialization:
[..see original..]
This is non-standard code. Specialisations are not allowed in the
class scope (as of now, anyway).
when the above code is compiled under gcc 4.1.0, compilation fails
with the error message "explicit specialization in non-namespace
scope". i have tried moving the specialization of the member
template outside of the class, but i can't find the right syntax.
[..see original..]
To specialise a member you *must* first specialise the class.
i've tried 100 variations to no avail and would be grateful for some
help.
Don't specialise. Overload:
template <class PointerClass>
class StorePointer {
public:
template <class RequestedPointerClass>
boost::shared_ptr<RequestedPointerClass> getPointer() const {
return
boost::dynamic_pointer_cast<RequestedPointerClass>(storedPointer_);
}
boost::shared_ptr<PointerClass> getPointer() const {
return storedPointer_;
}
private:
boost::shared_ptr<PointerClass> storedPointer_;
};
The compiler will know that you're calling the non-template version
when you omit the template argument:
myStorePointer.getPointer()
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]