转发声明库名 [英] Forward-declaring library names

查看:215
本文介绍了转发声明库名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

异常C ++的摘录:

在过去,你可以在这种情况下用class ostream;因为ostream过去是一个类,并且不在命名空间std。唉,没有更多。写class ostream;是非法的,有两个原因:

"In the old days, you could just replace "#include " with "class ostream;" in this situation, because ostream used to be a class and it wasn't in namespace std. Alas, no more. Writing "class ostream;" is illegal for two reasons:

ostream现在在命名空间std中,程序员不能声明任何存在于命名空间std中的东西。

ostream is now in namespace std, and programmers aren't allowed to declare anything that lives in namespace std.

ostream现在是一个模板的typedef;具体来说,它是typedef as basic_ostream。在任何情况下,basic_ostream模板都不能前向声明,但是你不能可靠地向前声明它,因为库实现允许做一些事情,比如添加自己的额外的模板参数(超出标准要求的),当然,您的代码不会知道 - 规则的主要原因,程序员不允许在namespace中编写自己的声明std 。

ostream is now a typedef of a template; specifically, it's typedef'd as basic_ostream. Not only would the basic_ostream template be messy to forward-declare in any case, but you couldn't reliably forward-declare it at all, because library implementations are allowed to do things like add their own extra template parameters (beyond those required by the standard), which, of course, your code wouldn't know about—one of the primary reasons for the rule that programmers aren't allowed to write their own declarations for things in namespace std."

我的问题:

以粗体显示。

感谢,

推荐答案

't forward declare ostream like this:

The part in bolds just says you can't forward declare ostream like this:

class ostream;




  • 因为ostream现在是typedef,声明的细节可以

  • 因为程序员不允许在命名空间std中声明任何内容(尽管它可以在大多数编译器上工作)。

  • 如果你想要一个ostream的forward声明,include< iosfwd>代替。 (或看看它的外观看起来像你的实现)。

    If you want a forward declaration of ostream, include <iosfwd> instead. (Or look inside what it looks like for your implementation).

    这篇关于转发声明库名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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