为什么在模板定义中不允许使用结构体? [英] Why are structs not allowed in template definitions?

查看:281
本文介绍了为什么在模板定义中不允许使用结构体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码产生错误错误:struct Foo不是模板常量参数的有效类型

template <struct Foo>
struct Bar {

};

为什么会这样?

template <class Foo>
struct Bar {

};

非常好,甚至接受一个结构作为参数。

works perfectly fine and even accepts an struct as argument.

推荐答案

这只是一个语法规则的工件 - 语法只是让你使用 class typename 关键字来指示类型模板参数。否则参数必须是一个非类型模板参数(基本上是一个整数,指针或引用类型)。

This is just an artifact of the syntax rules - the syntax just lets you use the class or typename keywords to indicate a type template parameter. Otherwise the parameter has to be a 'non-type' template parameter (basically an integral, pointer or reference type).

我想Stroustrup取自input)决定不需要将 struct 作为aa关键字来指示类型模板参数,因为不需要与C的向后兼容。

I suppose Stroustrup (and whoever else he might have taken input from) decided that there was no need to include struct as a a keyword to indicate a type template parameter since there was no need for backwards compatibility with C.

事实上,我的回忆(我回到家时必须做一些读书) typename 被添加来表示模板类型参数,Stroustrup会喜欢使用关键字为此目的(因为它是混乱)但是有太多的代码依赖它。

In fact, my recollection (I'll have to do some book readin' when I get back home) is that when typename was added to indicate a template type parameter, Stroustrup would have liked to take away using the class keyword for that purpose (since it was confusing), but there was too much code that relied on it.

编辑:

证明这个故事更像是(来自博客entry by Stan Lippman ):

Turns out the story is more like (from a blog entry by Stan Lippman):


这两个关键字的原因是
历史。在原始模板
规范中,Stroustrup重用了
现有类关键字来指定
类型参数,而不是引入
new关键字,这当然可以打破现有程序。这不是一个
新的关键字不被考虑 - 只是
,它认为不必要
考虑到其潜在的中断。并且在
之前上升到ISO-C ++标准,这是
是声明一个类型
参数的唯一方法。

The reason for the two keywords is historical. In the original template specification, Stroustrup reused the existing class keyword to specify a type parameter rather than introduce a new keyword that might of course break existing programs. It wasn't that a new keyword wasn't considered -- just that it wasn't considered necessary given its potential disruption. And up until the ISO-C++ standard, this was the only way to declare a type parameter.

关键词似乎
总是混乱。我们发现是
,初学者是[想知道]
是否使用类
约束或限制类型
参数用户可以指定为
类类型而不是说,一个
内置或指针类型。所以,有
有一种感觉,没有
介绍一个新的关键字是一个
的错误。

Reuses of existing keywords seems to always sow confusion. What we found is that beginners were [wondering] whether the use of the class constrained or limited the type arguments a user could specify to be class types rather than, say, a built-in or pointer type. So, there was some feeling that not having introduced a new keyword was a mistake.

在标准化过程中,某些
构造在
模板定义中发现,该定义解析为
表达式,尽管它们意味着
表示声明

During standardization, certain constructs were discovered within a template definition that resolved to expressions although they were meant to indicate declarations

...

委员会决定一个新的
关键字只是获得
编译器不幸的obsession
表达式的票。新关键字是
自描述类型名。

The committee decided that a new keyword was just the ticket to get the compiler off its unfortunate obsession with expressions. The new keyword was the self-describing typename.

...

由于关键字在工资单上,
heck,混乱造成了
原来的决定重用
类关键字。当然,鉴于
广泛的现有代码和
书籍和文章和会话以及
发布使用类关键字,他们
选择也保留支持
使用关键字。所以这是
为什么你有两个。

Since the keyword was on the payroll, heck, why not fix the confusion caused by the original decision to reuse the class keyword. Of course, given the extensive body of existing code and books and articles and talks and postings using the class keyword, they chose to also retain support for that use of the keyword as well. So that's why you have both.

这篇关于为什么在模板定义中不允许使用结构体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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