Home:ALL Converter>C++ Including STL within header files

C++ Including STL within header files

Ask Time:2013-05-29T12:53:45         Author:mr5

Json Formatter

Is it a bad or a good idea to include STL within a header file? 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. 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. 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).

  2. If I include the STL in my header file, what problem could exist? 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?

  4. And, can you give me an enlightenment why I should (should not) include STL in my header?

Author:mr5,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16805766/c-including-stl-within-header-files
Ram :

Use PIMPL idiom to create a compilation firewall on headers that expose / export STL types : Details\n\nclass MyList{\npublic:\n//Some functions\nprivate:\nstd::vector<int> _content;\n};\n\n\nIf 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.\n\n\nYour 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. \nYes, 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.\nNo problem as long as you have the runtime for the dll in the other computer.\nYou should not have stl types across dll/component boundaries for reusable code.\n",
2013-05-29T05:01:35
Raghuram :

If you are using Standard C++ STL library then you may not have porting issues as both Microsoft Visual C++ and g++ support these.\nUnless you you non standard STL headers then you will have issues.",
2013-05-29T04:57:10
Mark Garcia :

\nSo what are the effects in terms of code portability (for those who\nreally wanted their code to be platform independent) when forward\ndeclaring a type available on STL ?\n\nUsing standard C++ and the standard libraries at all times is the hallmark of portability.\n\nIf I include the STL in my header file, what problem could exist? And\nwill this affect the portability of my code?\n\nLonger compile time perhaps? And again, see the above answer.\n\nWhat if I include STL in the header file of my DLL and bring that DLL\nin other computers, what problem could I encounter?\n\nMostly and AFAIK, DLLs only "store" the method definitions of your classes. You still need to include the STL headers in your .h files.\n\nAnd, can you give me an enlightenment why I should (should not)\ninclude STL in my header?\n\nYou should, because you almost always want to use STL. Come to Lounge<C++> and you'll sure be enlightened.",
2013-05-29T05:02:09
yy