I have a quick question regarding the use of typedefs for lengthy templates. The crux: I've found myself in something of a pickle—there doesn't seem to be a good place to place typedefs except local to client functions. While there are similar SO questions (see here for example), none seem to address this exactly. Please note that this question doesn't address whether typedefs are desirable in what follows—I've tried to simplify things for expository purposes.
My problem has arisen while working with boost::shared_ptr<T>
. Basically, I want to do the following:
#include <boost/shared_ptr.hpp>
typedef boost::shared_ptr<Widget> WidgetPtr;
Placing this typedef in the Widget
declaration header seems ugly. There seem to be two considerations here: (i) if Widget
itself doesn't make use of shared pointers in its members, we've added an additional include (as we can't forward declare the boost::shared_ptr
template class—correct me if I'm wrong?) (ii) if we want to make use of this typedef during the declaration of another class (call that class Foo
) we violate best practices by including Widget.h
instead of simply forward declaring Widget
or including WidgetFwd.h
... unless this typedef is duplicated in the latter. Furthermore, it doesn't seem make sense to typedef boost::shared_ptr<Widget>
during the declaration of Widget
itself—we seem to be mixing Widget
's declaration with an anticipation of how clients will make use of the Widget
interface.
Okay, so that's bad, but this is worse: if I don't attempt some combination of the above I end up with duplicate typedefs in client code, which yields inconsistency (and hence, likely, error)—the whole point being that given Widget
, a WidgetPtr
typedef should act as a type in its own right. Example: we don't want Foo
to make use of one WidgetPtr
, a typedef of boost::shared_ptr
, while Bar
is using WidgetPtr as a typedef for std::auto_ptr
.
Another method (and one of the few that I've seen mentioned in online discussion) would be to make the typedef a public member of Widget
and then use Widget::Ptr
:
class Widget {
// ...
public:
typedef boost::shared_ptr<Widget> Ptr;
};
Again, I don't like this as (i) it suggests that the pointer type is somehow a member of the class and (ii) it leads to a wonky interface. Worse still: since every class that I write can potentially be pointed to using smart pointers, I end up chasing the imaginary client's tail. Ugly, ugly, ugly.
As it stands, I've removed the typedefs from this codebase (as they led to serious confusion, duplication) and re-introduced them locally in selected functions. Here again there's a problem with inconsistent usage but it's not quite as severe.
The only other solution I can think of—and again I'm not sure whether this is considered good practice—would be to have a utilities header in which the typedefs are placed, potentially within their own namespace. In this header we'd include and be done with it.
Am I missing something obvious or is this just plain tricky?
PS—Apologies for the length of the above; I couldn't find a simpler way of fully expressing the problem.
An object referenced by the contained raw pointer will not be destroyed until reference count is greater than zero i.e. until all copies of shared_ptr have been deleted. So, we should use shared_ptr when we want to assign one raw pointer to multiple owners. // referring to the same managed object.
"Shared pointer is a smart pointer (a C++ object wih overloaded operator*() and operator->()) that keeps a pointer to an object and a pointer to a shared reference count. Every time a copy of the smart pointer is made using the copy constructor, the reference count is incremented.
Admittedly, the std::shared_ptr is about two times slower than new and delete. Even std::make_shared has a performance overhead of about 10%.
The difference between the Unique Pointer and the shared Pointer is that unlike shared, where an object can have multiple shared pointers, only one unique pointer can be pointing to an object at any given time.
Furthermore, it doesn't seem make sense to typedef boost::shared_ptr during the declaration of Widget itself—we seem to be mixing Widget's declaration with an anticipation of how clients will make use of the Widget interface.
First of all, this is not at all wrong - after all, the means of how the clients will (and can) use the interface is part of the interface itself; and for C++, not being garbage-collected, memory management of objects is a rather crucial part of their interface.
So there are two cases. In one case, the Widget
would anticipate it would be used through a shared pointer. This would mean that eg. child widgets obtained from a widget are returned as shared_ptr
s, everywidget created has it shared_ptr
and so on. It would be totally legitimate to typedef WidgetPtr
in the same header as Widget
.
In the second case, Widget
s would expect to be managed eg. by ordinary new
and delete
. The clients can use shared_ptr
s in special cases, but nothing says eg. a printer dialogue routine can't use auto_ptr
instead. The clients have to be prepared that if wptr
is a shared_ptr
, the line
shared_ptr<Widget> w2(wptr->firstChild()->parent());
leads to a disaster.
Your question seems to indicate the latter is your case. So IMHO, what you've done is OK. The clients can choose their means of managing Widget
objects, as long as it doesn't affect other clients.
I don't like a library dictating the use of a particular smart pointer, but I tolerate it if it is necessary.
If you wish to force users to always use a shared_ptr
to manipulate a widget, it's impossible, so don't even bother trying.
On the other hand, if you have a method from Widget
which returns a boost::shared_ptr<Widget>
, then providing a (sane) typedef might simplify the client code.
I would therefore promote the use of an inner typedef:
class Widget
{
public:
typedef boost::shared_ptr<Widget> Ptr;
Ptr AccessFirstChild();
}; // class Widget
in which case it's perfectly okay to #include
the necessary headers.
You are overthinking this in my opinion. Everybody who wants to have a shared_ptr<Widget>
is going to have to include the Widget header file anyway. Putting the typedef
(which is a good idea imo) in Widget.h
makes 100% sense to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With