[英]C++ Including STL within header files
Is it a bad or a good idea to include STL within a header file?在头文件中包含 STL 是坏主意还是好主意? Wherein you use them as a member variable of your own defined class.
其中您将它们用作您自己定义的类的成员变量。
My assumption is, there are people who really wanted their created library to be very independent on C++ standard library.我的假设是,有些人真的希望他们创建的库非常独立于 C++ 标准库。 So they are forced to rewrite again a type similar to the functionality available in C++ STL while other's try to forward declare in their header file the type they will be needed later.
因此,他们被迫再次重写类似于 C++ STL 中可用功能的类型,而其他人则尝试在其头文件中转发声明稍后需要的类型。 Which is other's sees this as a bad practice and not a good idea at all.
其他人认为这是一种不好的做法,根本不是一个好主意。
Please correct me if I'm wrong (I don't know much that's why all is just an assumption):如果我错了,请纠正我(我知道的不多,这就是为什么一切都只是假设):
So what are the effects in terms of code portability (for those who really wanted their code to be platform independent) when forward declaring a type available on STL ?(I only know of a type of vector
as suggested by MSDN can be forward declared but not guaranteed to work at all times).那么在前向声明 STL 上可用的类型时,代码可移植性(对于那些真正希望他们的代码独立于平台的人)有什么影响?(我只知道 MSDN 建议的一种类型的
vector
可以被前向声明,但是不能保证一直工作)。
If I include the STL in my header file, what problem could exist?如果我在头文件中包含 STL,可能存在什么问题? And will this affect the portability of my code?
这会影响我的代码的可移植性吗?
What if I include STL in the header file of my DLL and bring that DLL in other computers, what problem could I encounter?如果我在我的 DLL 的头文件中包含 STL 并将该 DLL 带入其他计算机,我会遇到什么问题?
And, can you give me an enlightenment why I should (should not) include STL in my header?而且,你能给我一个启示,为什么我应该(不应该)在我的标题中包含 STL?
Use PIMPL idiom to create a compilation firewall on headers that expose / export STL types : Details使用 PIMPL 习惯用法在公开/导出 STL 类型的标头上创建编译防火墙: 详细信息
class MyList{
public:
//Some functions
private:
std::vector<int> _content;
};
If you create MyList in Vs2012 but the component is built in VS2008, then the code inside VS2008 will expect the memory layout as per STL 2008 but the layout will be that of STL 2012. This will create a whole host of issues.如果您在 Vs2012 中创建 MyList 但组件是在 VS2008 中构建的,那么 VS2008 中的代码将期望按照 STL 2008 的内存布局,但布局将是 STL 2012 的布局。这将产生一系列问题。
So what are the effects in terms of code portability (for those who really wanted their code to be platform independent) when forward declaring a type available on STL ?
那么在前向声明 STL 上可用的类型时,代码可移植性(对于那些真正希望他们的代码独立于平台的人)有什么影响?
Using standard C++ and the standard libraries at all times is the hallmark of portability.始终使用标准 C++ 和标准库是可移植性的标志。
If I include the STL in my header file, what problem could exist?
如果我在头文件中包含 STL,可能存在什么问题? And will this affect the portability of my code?
这会影响我的代码的可移植性吗?
Longer compile time perhaps?也许更长的编译时间? And again, see the above answer.
再次,请参阅上面的答案。
What if I include STL in the header file of my DLL and bring that DLL in other computers, what problem could I encounter?
如果我在我的 DLL 的头文件中包含 STL 并将该 DLL 带入其他计算机,我会遇到什么问题?
Mostly and AFAIK, DLLs only "store" the method definitions of your classes.大多数情况下和 AFAIK,DLL 仅“存储”您的类的方法定义。 You still need to include the STL headers in your
.h
files.您仍然需要在
.h
文件中包含 STL 头文件。
And, can you give me an enlightenment why I should (should not) include STL in my header?
而且,你能给我一个启示,为什么我应该(不应该)在我的标题中包含 STL?
You should, because you almost always want to use STL.你应该,因为你几乎总是想使用 STL。 Come to Lounge<C++> and you'll sure be enlightened.
来到Lounge<C++> ,您一定会大开眼界。
If you are using Standard C++ STL library then you may not have porting issues as both Microsoft Visual C++ and g++ support these.如果您使用的是标准 C++ STL 库,那么您可能不会遇到移植问题,因为 Microsoft Visual C++ 和 g++ 都支持这些。 Unless you you non standard STL headers then you will have issues.
除非您使用非标准 STL 标头,否则您将遇到问题。
Avoid STL in headers at all costs:不惜一切代价避免标头中的 STL:
How would STL improve the following API? STL 将如何改进以下 API? Consumers of IDog do not need to know the internal structure of a dog, only that it barks.
IDog 的消费者不需要知道狗的内部结构,只需要知道它会吠叫。
struct IDog
{
virtual void Bark() = 0;
virtual void Free() = 0;
}
IDog* CreateDog();
In your source code you might have
struct Dog: IDog
{
Dog() {...}
std::vector<VocalChord> vocalChords;
void Bark() override { Resonate(vocalChords); }
void Free() { delete this; }
};
IDog* CreateDog() { return new Dog(); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.