简体   繁体   English

什么是“模板”<typename from, typename tag> 结构别名;” 在 c++ 中?</typename>

[英]What is “template <typename From, typename Tag> struct Alias;” in c++?

The following is the code:以下是代码:

namespace o
{

template <typename From, typename Tag> struct Alias;
template <typename From, typename Tag>
inline std::ostream &operator<<(std::ostream &stream, const Alias<From, Tag> &inst);

template <typename From, typename Tag> struct Alias final
{ ...... } 
}

I was wondering, what is this template <typename From, typename Tag> struct Alias .我想知道,这个template <typename From, typename Tag> struct Alias是什么。 I know what is the meaning of a template function.我知道模板 function 是什么意思。 Is this a template struct or something like that?这是模板结构还是类似的东西?

was wondering, what is this template struct Alias.想知道,这个模板结构别名是什么。 I know what is the meaning of a template function.我知道模板 function 是什么意思。 Is this a template struct or something like that?这是模板结构还是类似的东西?

That's exactly what it is.这正是它的本质。

Specifically, that line is a forward declaration, to allow you to use the template struct in a function before you actually define what is in the struct.具体来说,该行是前向声明,允许您在实际定义结构中的内容之前在 function 中使用模板结构。

The following line(s):以下行:

<typename From, typename Tag> struct Alias final { ...... }

actually defines what's in the template struct.实际上定义了模板结构中的内容。

Template structs work similarly to template functions in the sense that the compiler generates a specific case of the struct when it needs it.模板结构的工作方式类似于模板函数,因为编译器会在需要时生成结构的特定情况。 Like functions, template structs act as a blueprint for the compiler to generate classes from the blueprint when it needs to.与函数一样,模板结构充当编译器的蓝图,以便在需要时从蓝图生成类。

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

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