On Wed, Jan 12, 2011 at 6:16 PM, David Brown <da...@westcontrol.com> wrote:
> On 12/01/2011 16:22, Achilleas Margaritis wrote:
>>
>> Hello all.
>>
>> I have a idea for automatic generation of headers in a c++ program.
>> Having to maintain headers is a very time consuming task, and I think
>> we will all benefit from such a thing. The idea is the following:
>>
>> Each time the compiler finds the pragma
>>
>> #pragma autoinclude("foo.hpp")
>>
>> it does the following:
>>
>> 1) searches the include path for the header foo.hpp.
>>
>> 2) if the header does not exist, then the compiler searches for the
>> file 'foo.cpp'
>>
>> 3) if the file 'foo.cpp' is found, then the header 'foo.hpp' is
>> generated automatically from the the .cpp file.
>>
>> 4) if the header exists, then the compiler compares the file dates of
>> the header and the implementation files. If the header is older than
>> the implementation file, then a new header is generated from the
>> implementation file.
>>
>> When the compiler finds a declaration in a .cpp file that is 100% the
>> same as a declaration in a header file, then the declaration in the
>> implementation file is ignored, because it has been created
>> automatically in the header by the compiler. If there is a difference,
>> then an error is declared.
>>
>> Example:
>>
>> file main.cpp:
>>
>> #pragma autoinclude ("foo.hpp")
>>
>> int main() {
>>     Foo *foo = new Foo;
>>     foo->bar();
>>     foos.push_back(foo);
>> }
>>
>> file foo.cpp:
>>
>> #include<list>
>>
>> class Foo {
>> public:
>>     void bar() {
>>     }
>> };
>>
>> std::list<Foo *>  foos;
>>
>> static int data = 0;
>>
>> The compiler generates the following header:
>>
>> #ifndef FOO_HPP
>> #define FOO_HPP
>>
>> #include<list>
>>
>> class Foo {
>> public:
>>     void bar();
>> };
>>
>> extern std::list<Foo *>  foos;
>>
>> #endif //FOO_HPP
>>
>> If such a feature existed, it would greatly speed up application
>> development for c++ applications.
>>
>
> I can see how such a feature could be useful, but is there any reason why it
> should be part of gcc, rather than a separate program that takes a cpp file
> and generates an hpp file?  Such a program would be much easier to develop
> and maintain than a new gcc pragma, and more flexible (for example, it could
> be used with other compilers).  Your timestamp checking features are easily
> accomplished with make.
>
>

The GCC contains all the necessary code for parsing C++. An external
program would have to use a custom parser (which is a lot of work,
since C++ is a very complex language), or LLVM (which may or may not
be 100% compatible with GCC yet, and which may or may not contain all
the GCC features). Doing it from within GCC guarantees code reuse and
consistency with the compiler, plus compatibility with the new c++
standard, which GCC already largely implements.

Furthermore, the compiler would need to handle the clash of
declarations between the generated header and the implementation file.
As any compiler is implemented right now, declaring the same class
twice will generate an error.

Additionally, providing such a functionality through GCC would not
have the maintenance overhead of an externally managed application:
once GCC is updated to a version with this functionality, this
functionality would become instantly available.

Finally, GCC is one of the most visible projects when it comes to c++
compilers. If GCC had such a functionality, I think that many people
would jump to the opportunity to use this functionality, and therefore
it could be part of the language standard in the far future.

Reply via email to