Struct与C ++中的模板变量 [英] Struct with template variables in C++
本文介绍了Struct与C ++中的模板变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在玩模板。我不想重塑std :: vector,我想在C ++中掌握模板。
我可以这样做吗?
template< typename T>
typedef struct {
size_t x;
T * ary;
} array;
我想要做的是一个基本的模板版本:
typedef struct {
size_t x;
int * ary;
} iArray;
如果我使用类而不是struct,它看起来像是工作,所以不能使用typedef structs?
解决方案
问题是你不能模板化一个typedef,也没有必要在C ++中typedef structs。 / p>
以下内容可以满足您的需要
template< typename T> ;
struct array {
size_t x;
T * ary;
};
I'm playing around with templates. I'm not trying to reinvent the std::vector, I'm trying to get a grasp of templateting in C++.
Can I do the following?
template <typename T>
typedef struct{
size_t x;
T *ary;
}array;
What I'm trying to do is a basic templated version of:
typedef struct{
size_t x;
int *ary;
}iArray;
It looks like it's working if I use a class instead of struct, so is it not possible with typedef structs?
解决方案
The problem is you can't template a typedef, also there is no need to typedef structs in C++.
The following will do what you need
template <typename T>
struct array {
size_t x;
T *ary;
};
这篇关于Struct与C ++中的模板变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文