use*_*536 5 c++ templates typedef
我有一个模板课
template <T>
class Example
{
...
};
Run Code Online (Sandbox Code Playgroud)
其中有许多以下类型的方法:
template <class U> <class V> method(....)
Run Code Online (Sandbox Code Playgroud)
在这些内部我使用tr1 :: shared_ptr到U或V或T.
它繁琐的打字tr1::shared_ptr<const U>
或tr1::shared_ptr<const V>
.
显而易见的事情:
template <typename U>
typedef tr1::shared_ptr<U> shptr<U>;
Run Code Online (Sandbox Code Playgroud)
不起作用.
你在这种情况下做了什么?什么能减少冗长的东西?
您可以使用内部类型:
template <typename U>
struct sptr {
typedef tr1::shared_ptr<U> t;
};
Run Code Online (Sandbox Code Playgroud)
然后说sptr<U>::t
,或者不幸的是经常typename sptr<U>::t
.
C++ 0x有模板typedef,您可以检查是否可以说服您的编译器接受它们:
template<typename U>
using sptr = tr1::shared_ptr<U>;
Run Code Online (Sandbox Code Playgroud)
然后说 sptr<U>
当然#define sptr ::tr1::shared_ptr
,例如,如果您将来期待C++ 0x并希望缩小差距,那么总会如此.或者,如果你在一个足够狭窄的环境中使用它,那么宏并不可怕.