Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, removing #include<vector> or #include<string> in class header

I want to remove, if possible, the includes of both <vector> and <string> from my class header file. Both string and vector are return types of functions declared in the header file.

I was hoping I could do something like:

namespace std {
    template <class T>
    class vector;
}

And, declare the vector in the header and include it in the source file.

Is there a reference covering situations where you must include in the header, and situations where you can pull the includes into the source file?

like image 259
bias Avatar asked Nov 26 '22 21:11

bias


1 Answers

You cannot safely forward declare STL templates, at least if you want to do it portably and safely. The standard is clear about the minimum requirements for each of the STL element, but leaves room for implemtation extensions that might add extra template parameters as long as those have default values. That is: the standard states that std::vector is a template that takes at least 2 parameters (type and allocator) but can have any number of extra arguments in a standard compliant implementation.

What is the point of not including string and vector headers? Surely whoever is going to use your class must have already included it since it is on your interface.

When you ask about a reference to decide when to include and when to forward declare, my advice would be: include everything that is part of your interface, forward declare internal details.

There are more issues here that plain compilation performance. If you push the include of a type that is in your public (or protected) interface outside of the header you will be creating dependencies on the order of includes. Users must know that they must include string before including your header, so you are giving them one more thing to worry about.

What things should be included in the implementation file: implementation details, loggers, elements that don't affect the interface (the database connectors, file headers), internal implementation details (i.e. using STL algorithms for your implementation does not affect your interface, functors that are created for a simple purpose, utilities...)

like image 156
David Rodríguez - dribeas Avatar answered Dec 11 '22 12:12

David Rodríguez - dribeas