Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good style to hide shared_ptr behind a typedef?

Tags:

I'd like to reduce some visual noise in the code and hide shared_ptr behind a typedef like this:

typedef boost::shared_ptr<SomeLongClass> SomeLongClassPtr;

So this:

void foo(const boost::shared_ptr<SomeLongClass>& a,
         boost::shared_ptr<SomeLongClass>& b);

becomes this:

void foo(const SomeLongClassPtr& a, SomeLongClassPtr& b);

On the other hand I'm worried that I'm reducing the explicitness of the code.

Which is a better style?

like image 971
Alex B Avatar asked Aug 09 '10 00:08

Alex B


2 Answers

Given that std::string is itself a typedef, I think you are fine. I do it myself.

Even Scott Meyers recommends typedef for ease of reading code in cases like yours.


EDIT: Effective C++, Second Edition, Page 120, Item 28, fourth paragraph. "...provide typedefs that remove the need..."

More Effective C++, 7th printing, Page 237, Item 31 First paragraph.

More Effective C++, 7th printing, Page 238, Item 31 First paragraph after code sample.


In essence, no worries. :-)

like image 185
JustBoo Avatar answered Sep 24 '22 06:09

JustBoo


We use TypePtr typedefs in our code for shared_ptr<Type> objects. It is also useful to have a TypeConstPtr for shared_ptr<const Type>.

like image 41
user3436624 Avatar answered Sep 23 '22 06:09

user3436624