1

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?

mr5
  • 3,438
  • 3
  • 40
  • 57
  • 1
    Nitpick: [the STL is not the C++ Standard Library](http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all-about) – Cornstalks May 29 '13 at 05:04
  • Its all assumption. I'm still new in C++. Just said it because I'm running out of terms to use. – mr5 May 29 '13 at 05:42

4 Answers4

2

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.

  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.
  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.
  3. No problem as long as you have the runtime for the dll in the other computer.
  4. You should not have stl types across dll/component boundaries for reusable code.
Community
  • 1
  • 1
Ram
  • 3,045
  • 3
  • 27
  • 42
  • 1
    source will be portable but not the components. – Ram May 29 '13 at 05:12
  • @Ram Then, does it mean that one will rewrite again the code(data type from STL, one wanted to) for he/she be able to attain code(?) and platform portability? – mr5 May 29 '13 at 06:50
2

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.

Community
  • 1
  • 1
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
  • I believe there's sometimes an issue with the C++ STL as opposed to other parts of the standard library. From what I hear, it seems to be the main part that platforms don't support (those which are for embedded systems if I'm not mistaken). – chris May 29 '13 at 05:04
  • 1
    @chris If he can't use STL in his favorite implementation, then the question doesn't apply, isn't it? – Mark Garcia May 29 '13 at 05:07
  • 1
    @chris: compilers targeting embedded systems often depart from the C++ standard (and thus don't offer "real" C++ implementations). If you're targeting an embedded system, chances are the standard library is just a small part of the *many* things you have to worry about. – Cornstalks May 29 '13 at 05:07
  • Yes, I'm merely recalling what I've picked up on, and the C++ STL gives the sense of being the biggest portability issue from browsing SO. I have nil actual experience in the area and haven't particularly done any research toward it, so there's a lot about it I don't know. – chris May 29 '13 at 05:10
1

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.

Raghuram
  • 3,937
  • 2
  • 19
  • 25
1

Avoid STL in headers at all costs:

  1. STL is full of implementation details. Leave that to source code.
  2. Headers are your API, you only want to be exposing functions, structs and interfaces.
  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.
  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.

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(); }
  • Just because part of the implementation exists in a header file, doesn't mean it is exposed to the user. It is only considered exposed if you want to modify a dll in a way that is incompatible with the header, without recompiling whatever uses it. So by hiding something that many already considers hidden, you have forced another major implementation detail on the user: Dog is always stored on the heap, and cannot automatically be destroyed when not in use. – HAL9000 Jul 17 '20 at 22:37
  • I generally break down classes into those needed on the heap and those on the stack. Typically the vast majority of classes will be heap based. Anything that is transient, likely to cause fragmentation or interferes with real-time processing will get some stack based solution appropriate for the usage. – TheAbstraction Jul 19 '20 at 06:24