C++ include necessity checker?

Started by
23 comments, last by Hodgman 16 years ago
Quote:Original post by Ezbez
Quote:Original post by Nypyren
If inclusion of a header alters the meaning of previous definitions (via macros) in such a way that the resulting behavior differs, then the project is poorly constructed anyway.


Ah, but the point of this is to fix poor construction, no? That it's poorly constructed is pretty much a prerequisite for doing this.


Simply including a header that you don't actually need (maybe because you removed all the code that originally needed it) is very different from including a header that has a "#define class struct" or something else dangerous but perfectly possible like that.
Advertisement
Quote:Original post by Nypyren
If inclusion of a header alters the meaning of previous definitions (via macros) in such a way that the resulting behavior differs, then the project is poorly constructed anyway.
Who needs macros?
//a.hnamespace a{	void foo();}// a.cpp#include <iostream>namespace a{	void foo()	{		std::cout << "hello\n";	}}// b.hnamespace a{	namespace b	{		void foo();	}}// b.cpp#include <iostream>namespace a{	namespace b	{		void foo()		{			std::cout << "goodbye\n";		}	}}// c.hnamespace a{	namespace b	{		void bar();	}}// c.cpp#include "a.h"#include "b.h"namespace a{	namespace b	{		void bar()		{			foo();		}	}}// d.hnamespace a{	namespace b	{		void baz();	}}// d.cpp#include "b.h"#include "a.h"namespace a{	namespace b	{		void baz()		{			foo();		}	}}// main.cpp#include "c.h"#include "d.h"int main(){	a::b::bar();	a::b::baz();}

Σnigma
While it can happen with namespaces, it's more likely to occur with function overloads, especially overloaded operators and template functions.
Overloaded new operators are probably the most dangerous issue with my idea of just removing headers until it stops compiling. Perhaps in addition to the "make sure it compiles" is to have a set of test cases to validate behavior as well.

Let's abandon the header-removal idea and go with precompiled headers or conglomerate compiling (including all CPP files in each directory in a single CPP file and then only compiling each conglomerate).
Quote:Original post by Nypyren
Let's abandon the header-removal idea and go with precompiled headers or conglomerate compiling (including all CPP files in each directory in a single CPP file and then only compiling each conglomerate).

But if the original purpose of header-removal was to reduce compile times, then both of those solutions can do more harm than good in a lot of situations...

This topic is closed to new replies.

Advertisement