Is using macros to abbreviate long winded boost template names a bad practice?
I am tired of writing shared_ptr<>开发者_如何转开发, it's lengthening the code lines tremendously. Any reason not to do this?
Why use macro's? Such a mechanism already exists:
typedef boost::shared_ptr<some_longer_name_omg_help> pointer_type;
pointer_type p; // phew
If you're asking about what Steve is suggesting in the comments, you could try:
template <typename T>
struct sp
{
typedef boost::shared_ptr<T> type;
};
typedef sp<some_longer_name_omg_help>::type pointer_type;
But I don't know if it's that much shorter.
I think your macro use is a sign of laziness, honestly. boost::shared_ptr
is, in almost any reasonable measure of "lengthy", not lengthy at all. All you're doing is destroying readability.
I've got a much simpler suggestion: Use fewer shared_ptr
's. If you use them so much, it sounds like you're blindly relying on them instead of a garbage collector. C++ requires you to think about object ownership, but if you do, most smart pointers can be replaced with simple references, which are not long-winded.
Do you really need shared ownership? If you stop and think for a few minutes, I'm sure you can pinpoint one owner of the object, and a number of users of it, that will only ever use it during the owner's lifetime. So simply make it a local/member object of the owners, and pass references to those who need to use it.
Admittedly, this doesn't solve the problem in the general case (there are many other long-winded template names), but in this specific case, I think it's the best solution.
One reason not to do this would be for new developer productivity. If you hire a new employee, it may be confusing for him to learn the intricacies of how you've changed the standard boost API.
Of course, you might say, "but we'll only do it for this one type" ... to that I say, "watch out for that slippery slope" ;-)
精彩评论