繁体   English   中英

是否有可能以任何方式(使用C ++预处理器等)替换shared_ptr <T> 含T $,weak_ptr <T> 具有T%和unique_ptr <T> 用T?

[英]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?

到目前为止,似乎答案是否定的。

鉴于整个位置的shared_ptrs了更多视觉上嘈杂的代码,这是不幸的。

使用C ++ 11,现在做到这一点的一种可能方法是使用别名 (比宏更干净的imo)。 例如,对于共享指针,您可以执行以下操作:

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

然后,如下所示使用它:

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

编辑: 实时示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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