Forward Declarations
来源:互联网 发布:旅行者一号 知乎 编辑:程序博客网 时间:2024/05/22 09:48
Forward declarations are a great way to eliminate needless compile-time dependencies. But here's an example of a forward-declaration snare... how would you avoid it?
Problem
JG Question
1. Forward declarations are very useful tools. In this case, they don't work as the programmer expected. Why are the marked lines errors?
// file f.h #ifndef XXX_F_H_ #define XXX_F_H_ class ostream; // error class string; // error string f( const ostream& ); #endif
Guru Question
2. Without including any other files, can you write the correct forward declarations for ostream and string above?
Solution
1. Forward declarations are very useful tools. In this case, they don't work as the programmer expected. Why are the marked lines errors?
// file f.h #ifndef XXX_F_H_ #define XXX_F_H_ class ostream; // error class string; // error string f( const ostream& ); #endif
Alas, you cannot forward-declare ostream and string this way because they are not classes... both are typedefs of templates.
(True, you used to be able to forward-declare ostream and string this way, but that was many years ago and is no longer possible in Standard C++.)
2. Without including any other files, can you write the correct forward declarations for ostream and string above?
Unfortunately, the answer is that there is no standard and portable way to do this. The standard says:
It is undefined for a C++ program to add declarations or definitions to namespace std or namespaces within namespace std unless otherwise specified.
Among other things, this allows vendors to provide implementations of the standard library that have more template parameters for library templates than the standard requires (suitably defaulted, of course, to remain compatible).
The best you can do (which is not a solution to the problem "without including any other files") is this:
#include <iosfwd> #include <string>
The iosfwd header does contain bona fide forward declarations. The string header does not. This is all that you can do and still be portable. Fortunately, forward-declaring string and ostream isn't too much of an issue in practice since they're generally small and widely-used. The same is true for most standard headers. However, beware the pitfalls, and don't be tempted to start forward-declaring templates -- or anything else -- that belongs to namespace std... that's reserved for the compiler and library writers, and them alone.
- Forward Declarations
- forward declarations in c++
- ice slice Forward Declarations
- Forward declarations(声明),c++声明的一些问题!
- declarations
- forward
- forward
- Complicated Declarations
- 10.3 Declarations
- complicated declarations
- Variable Declarations
- Method Declarations
- 16.2 Namespace declarations
- 16.5 Type declarations
- 17.1 Class declarations
- 17.1 Class declarations
- 18.1 Struct declarations
- 20.1 Interface declarations
- 周爱民对于程序员的技术发展和职业规划给大家的一些建议
- 字符串指针与字符数组
- 世界近代三大数学难题之一费马定理http://lm2000i.bokee.com/viewdiary.12187540.html
- 专家推荐:学习Linux的九条忠告
- 费马大定理http://baike.baidu.com/view/124599.htm
- Forward Declarations
- 关于ResultSet can not re-read row data for column 2 错误及其原因
- 使用 Linux 系统调用的内核命令
- JMS(Jboss Messaging)的一点使用心得(三)Spring扩展应用-Security
- Linux 系统审计样例
- org.hibernate.NonUniqueObjectException
- Linux 内核剖析from ibm
- Struts标签
- 在网站中繁体中文和简体中文相互转换的实现