简体   繁体   English

C++ 在头文件中包含 STL

[英]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):如果我错了,请纠正我(我知道的不多,这就是为什么一切都只是假设):

  1. 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可以被前向声明,但是不能保证一直工作)。

  2. If I include the STL in my header file, what problem could exist?如果我在头文件中包含 STL,可能存在什么问题? And will this affect the portability of my code?这会影响我的代码的可移植性吗?

  3. 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 带入其他计算机,我会遇到什么问题?

  4. 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 的布局。这将产生一系列问题。

  1. Your component will not be portable across compilers let alone platforms.您的组件不能跨编译器移植,更不用说平台了。 A component built in VS2008 using std::vector as a member variable will have a different size to the same compiled in VS2012 for example.例如,使用 std::vector 作为成员变量在 VS2008 中构建的组件将具有与在 VS2012 中编译的相同大小不同的大小。
  2. Yes, your code will be compatible across compilers in most scenarios except when you are using features of STL that is more up to date in older versions.是的,在大多数情况下,您的代码将在编译器之间兼容,除非您使用旧版本中更新的 STL 功能。
  3. No problem as long as you have the runtime for the dll in the other computer.只要您在另一台计算机上有 dll 的运行时就没有问题。
  4. You should not have stl types across dll/component boundaries for reusable code.对于可重用代码,您不应该跨 dll/组件边界使用 stl 类型。

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:

  1. STL is full of implementation details. STL 充满了实现细节。 Leave that to source code.把它留给源代码。
  2. Headers are your API, you only want to be exposing functions, structs and interfaces.标头是您的 API,您只想公开函数、结构和接口。
  3. You may want to compile and link libraries in different modes - that leads to cross module errors if you are say allocating in debug and deleting memory in release.您可能希望以不同的模式编译和链接库 - 如果您说在调试中分配并在发布中删除内存,则会导致跨模块错误。 So do not use std::string to pass information across the API.所以不要使用 std::string 在 API 中传递信息。
  4. One you start to include just one header from STL you will find other modules start to leak in. You will end up with STL poisoning everything, no real interface and build times in minutes when it should be in seconds.一个你开始只包含来自 STL 的一个头文件,你会发现其他模块开始泄漏。你最终会被 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM