Initially I was trying to typedef a template class and I got to the “gotw 79” article.
And I didn’t want to create another class so I ended up doing the following. Basically typedef’ing inside the same class. It works obviously. but is it a good practice?
template <typename T,typename L>
class MyClass{
typedef std::tr1::shared_ptr<MyClass<T,L> > shrdPtr;
}
Thank you.
Well, I’m not a big fan of it unless you are designing
MyClassto be specifically used only within shared_ptr objects, at which point I would insist that requirement be enforced.It’s a little ridiculous to put typedefs for every unrelated template instantiation that you might use with a given object. Just because you might put
MyClassin a shared_ptr is not a good reason to typedef it there. You going to put typedefs for std::vector, map, list, unordered_map, set, deque,….etc, etc, etc?But if
MyClassextends shared_from_this and has private/protected constructors so that it can ONLY be created and immediately assigned to a shared_ptr then…sure…it’s part of the interface.If you’re trying to avoid having to type out long parameter lists to instantiate a shared_ptr for a templated type with lots of parameters then a better bet is an EXTERNAL utility object just like shown in the article you cited: