C ++,删除#include< vector>或#include< string>在类标题中 [英] C++, removing #include<vector> or #include<string> in class header

查看:176
本文介绍了C ++,删除#include< vector>或#include< string>在类标题中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能,我想删除< vector>的包含内容和< string>从我的类头文件。字符串和向量都是在头文件中声明的函数的返回类型。

I want to remove, if possible, the includes of both <vector> and <string> from my class header file. Both string and vector are return types of functions declared in the header file.

我希望我可以做类似的事情:

I was hoping I could do something like:

namespace std {
    template <class T>
    class vector;
}

并且,在标头中声明向量并将其包含在源文件中。

And, declare the vector in the header and include it in the source file.

是否有参考文献涵盖您必须包含在标题中的情况,以及您可以将包含纳入源文件的情况?

Is there a reference covering situations where you must include in the header, and situations where you can pull the includes into the source file?

推荐答案

您无法安全地转发声明STL模板,至少如果您想要便携且安全地执行它。该标准清楚地表明了每个STL元素的最低要求,但为实现扩展留出了空间,可以添加额外的模板参数,只要它们具有默认值。也就是说:标准规定std :: vector是一个模板,它至少需要2个参数(类型和分配器),但在符合标准的实现中可以有任意数量的额外参数。

You cannot safely forward declare STL templates, at least if you want to do it portably and safely. The standard is clear about the minimum requirements for each of the STL element, but leaves room for implemtation extensions that might add extra template parameters as long as those have default values. That is: the standard states that std::vector is a template that takes at least 2 parameters (type and allocator) but can have any number of extra arguments in a standard compliant implementation.

不包括字符串和向量标题有什么意义?当然,无论谁打算使用你的课程,都必须已经包含它,因为它在你的界面上。

What is the point of not including string and vector headers? Surely whoever is going to use your class must have already included it since it is on your interface.

当你询问一个引用来决定何时包含它以及何时转发时声明,我的建议是:包括你的界面的所有内容,转发声明内部细节。

When you ask about a reference to decide when to include and when to forward declare, my advice would be: include everything that is part of your interface, forward declare internal details.

这里有更多的问题,简单的编译性能。如果您推送在标头之外的公共(或受保护)接口中的类型的包含,您将创建包含顺序的依赖项。在包含标题之前,用户必须知道他们必须包含字符串,因此您需要再给他们一个担心。

There are more issues here that plain compilation performance. If you push the include of a type that is in your public (or protected) interface outside of the header you will be creating dependencies on the order of includes. Users must know that they must include string before including your header, so you are giving them one more thing to worry about.

应该做什么包含在实现文件中:实现细节,记录器,不影响接口的元素(数据库连接器,文件头),内部实现细节(即使用STL算法实现不会影响您的界面,创建的仿函数)为了一个简单的目的,公用事业......)

What things should be included in the implementation file: implementation details, loggers, elements that don't affect the interface (the database connectors, file headers), internal implementation details (i.e. using STL algorithms for your implementation does not affect your interface, functors that are created for a simple purpose, utilities...)

这篇关于C ++,删除#include&lt; vector&gt;或#include&lt; string&gt;在类标题中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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