为模板类创建类型别名 [英] Creating a type alias for a templated class

查看:105
本文介绍了为模板类创建类型别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是使用

std::vector<Object> ObjectArray;

我想要

MyArray<Object> ObjectArray;

保存所有std :: vector方法。 (例如push_back(),reserve(),...等)

with all the std::vector methods preserved. (like push_back(), reserve(), ...etc)

但是,使用

typedef std::vector MyArray;

无效。我应该使用模板?如何?

won't work. Should I use template instead? How?

推荐答案

你真正想要的是一个模板化的typedef。不幸的是,在当前版本的C ++中不支持,但是它们将被添加到C ++ 0x中。

What you would really want is a templated typedef. Unfortunately those are not supported in the current version of C++, but they will be added in C++0x.

现在,这里有一个可能的解决方法:

For now, here's a possible workaround:

template<class T> struct My {
    typedef std::vector<T> Array;
};

My<Object>::Array ObjectArray

优于直接使用 std :: vector ,我会让你决定。

Whether or not that is better than simply using std::vector directly, I'll leave to you to decide.

这篇关于为模板类创建类型别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆