简体   繁体   中英

Is it possible in any way (using C++ preprocessor etc) to replace shared_ptr<T> with T$, weak_ptr<T> with T%, and unique_ptr<T> with T?

So far it seems like the answer is no.

Which is unfortunate given how much more visually noisy code becomes with shared_ptrs all over the place.

With C++11, one possible way to do that now is to use aliases (which are cleaner imo than macros). Eg for shared pointers, you could do:

template<typename T>
using Shared = std::shared_ptr<T>;

Then, use it like the following:

Shared<int> myInt; // Is in fact a std::shared_ptr<int>

EDIT: Live example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM