繁体   English   中英

键入一个模板类,而不指定模板参数

[英]Typedef a template class without specifying the template parameters

我正在尝试输入undedered_map或std :: map,这取决于是否有可用的TR1库。 但我不想指定模板参数。 从我到目前为止所读到的,在官方c ++ 0x标准可用之前,不能使用不带参数的typedef'ing模板。 那么有谁知道这个优雅的解决方法?

#ifdef _TR1
#include <unordered_map> 
typedef std::tr1::unordered_map MyMap; //error C2976: too few template arguments
#else
#include <map> 
typedef std::map MyMap; //error C2976: too few template arguments
#endif

我看到这样做的方法是将typedef包装在template-struct中:

template<typename KeyType, typename MappedType>
struct myMap
{
#ifdef _TR1
    typedef std::tr1::unordered_map<KeyType, MappedType> type;
#else
    typedef std::map<KeyType, MappedType> type;
#endif
};

然后在您的代码中调用它,如下所示:

myMap<key, value>::type myMapInstance;

它可能比你想要的更冗长,但我相信它满足了当前C ++状态的需要。

您必须为typedef使用完整类型。

请改用#define宏。

暂无
暂无

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

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