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):
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).
If I include the STL in my header file, what problem could exist? 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?
And, can you give me an enlightenment why I should (should not) include STL in my header?
Use PIMPL idiom to create a compilation firewall on headers that expose / export STL types : Details
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.
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 ?
Using standard C++ and the standard libraries at all times is the hallmark of portability.
If I include the STL in my header file, what problem could exist? 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?
Mostly and AFAIK, DLLs only "store" the method definitions of your classes. You still need to include the STL headers in your .h
files.
And, can you give me an enlightenment why I should (should not) include STL in my header?
You should, because you almost always want to use STL. Come to Lounge<C++> and you'll sure be enlightened.
If you are using Standard C++ STL library then you may not have porting issues as both Microsoft Visual C++ and g++ support these. Unless you you non standard STL headers then you will have issues.
Avoid STL in headers at all costs:
How would STL improve the following API? Consumers of IDog do not need to know the internal structure of a dog, only that it barks.
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(); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.