C++ 中带有模板变量的结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2448242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:31:18  来源:igfitidea点击:

Struct with template variables in C++

c++classtemplatesstruct

提问by monkeyking

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++.

我在玩模板。我不是要重新发明 std::vector,而是要掌握 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?

如果我使用类而不是结构,看起来它可以工作,那么使用 typedef 结构是不可能的吗?

回答by Binary Worrier

The problem is you can't template a typedef, also there is no need to typedef structs in C++.

问题是你不能对 typedef 进行模板化,也不需要在 C++ 中对结构进行 typedef。

The following will do what you need

以下将做你需要的

template <typename T> 
struct array { 
  size_t x; 
  T *ary; 
}; 

回答by Andrey

template <typename T>
struct array {
  size_t x;
  T *ary;
};

回答by dirkgently

You don't need to do an explicit typedeffor classes and structs. What do you need the typedeffor? Further, the typedefafter a template<...>is syntactically wrong. Simply use:

您不需要typedef对类和结构进行显式操作。你需要什么typedef?此外,typedefafter atemplate<...>在语法上是错误的。只需使用:

template <class T>
struct array {
  size_t x;
  T *ary;
} ;

回答by sepp2k

You can template a struct as well as a class. However you can't template a typedef. So template<typename T> struct array {...};works, but template<typename T> typedef struct {...} array;does not. Note that there is no need for the typedef trick in C++ (you can use structs without the structmodifier just fine in C++).

您可以对结构和类进行模板化。但是,您不能对 typedef 进行模板化。所以template<typename T> struct array {...};有效,但template<typename T> typedef struct {...} array;没有。请注意,在 C++ 中不需要 typedef 技巧(您可以在 C++ 中使用没有struct修饰符的结构体)。

回答by Johannes Schaub - litb

The Standard says (at 14/3. For the non-standard folks, the names following a class definition body (or the type in a declaration in general) are "declarators")

标准说(在 14/3。对于非标准的人,类定义主体(或一般声明中的类型)后面的名称是“声明符”)

In a template-declaration, explicit specialization, or explicit instantiation the init-declarator-list in the dec-laration shall contain at most one declarator. When such a declaration is used to declare a class template, no declarator is permitted.

在模板声明、显式专业化或显式实例化中,声明中的 init-declarator-list 最多应包含一个声明符。当这样的声明用于声明类模板时,不允许使用声明符。

Do it like Andrey shows.

像安德烈秀那样做。

回答by webgenius

The syntax is wrong. The typedefshould be removed.

语法错误。本typedef应该被删除。

回答by Narfanator

From the other answers, the problem is that you're templating a typedef. The only "way" to do this is to use a templated class; ie, basic template metaprogramming.

从其他答案来看,问题在于您正在对 typedef 进行模板化。做到这一点的唯一“方法”是使用模板化类;即,基本模板元编程。

template<class T> class vector_Typedefs {
    /*typedef*/ struct array { //The typedef isn't necessary
        size_t x; 
        T *ary; 
    }; 

    //Any other templated typedefs you need. Think of the templated class like something
    // between a function and namespace.
}

//An advantage is:
template<> class vector_Typedefs<bool>
{
    struct array {
        //Special behavior for the binary array
    }
}