Is it wrong to use typedef smart pointer?

I use smart pointers in my current project, and it seems very cumbersome to type long lines of code when using them.

Since I wanted my code to be cleaner and easier to use, I started introducing such smart pointers:

typedef std::unique_ptr<System> SystemPtr; 

So my question is, is this a bad practice for a typedef smart pointer?

+5
source share
2 answers

There is nothing wrong with that, but your choice of name is terrible. Someone reads that he has no idea if this is a generic pointer, a unique pointer, an intrusive pointer to a link count or just a raw pointer to a System .

If you really need brevity,

 template<class T>using up=std::unique_ptr<T>; 

is another symbol at the point of use than your up<System> plan, and makes it more clear that it is a unique pointer and does not require a typedef for each type. Plus, in some cases, this leads to puns.

+10
source

As already mentioned, there is nothing syntactically wrong with this. I just wanted to add that for hunting or relying on something like Intellisense to easily find a definition in larger projects can be frustrating. Programmers rely on being in the zone to do their best. Even something as simple as 60 seconds to track typedef can ruin a groove.

For such reasons, I feel that in situations like yours, and just generally, it is better not to apply typedefs liberally. You should be able to quickly and easily find the type of variable by going to the top of the associated area.

And I must say that your type is not so long (high-level meta-types can be long several lines).

+1
source

All Articles