【发布时间】:2019-09-17 21:44:52
【问题描述】:
我有一些这样的代码:
#include <string>
#include <map>
typedef std::less<std::string> Comparator; // simplified
typedef std::allocator<std::pair<const std::string, int>> Allocator; // simplified
template<class T>
struct Base
{
typedef std::map<std::string, T, Comparator, Allocator> type; // VERY long declaration in the actual code
};
template<class T>
struct Container : public Base<T>::type
{
Container(Allocator a) : Base<T>::type(Comparator(), a) {}
};
int main()
{
Allocator a;
Container<int> c(a);
}
虽然在我的实际代码中声明有点花哨。
使用了 Base 结构,这样我就不必多次编写长 map 声明。
我想知道是否有更好的方法可以在没有任何 Base 结构的情况下从地图继承?
请不要使用宏。我希望以某种方式将 typedef 隐藏在 Container 类本身或类似的东西中。
谢谢,
【问题讨论】:
标签: c++ templates inheritance c++14