Re: future of the C++
Andre Kaufmann wrote:
Joshua Maurice wrote:
On Jul 4, 10:41 am, Andre Kaufmann <akfmn...@t-online.de> wrote:
On 04.07.2010 00:50, Dragan Milenkovic wrote:
However, what would be really nice is something to make
the pimlp idiom obsolete. :-D
Yep, a module concept is IMHO badly needed in C++. Unfortunately it has
been removed from the first C++0x proposals.
Can you point at any proposals or ideas? I'm not sure how "modules"
would remove the need for pimpl. pimpl is an idiom whose goal is to
Why is pimpl needed in C++ ?
As you already correctly stated to decouple source code and to increase
compilation speed.
But why can't that be done by the compiler automatically ?
Why do we have to use such an old stone age relict of code decoupling
like header files and pimpl in C++ ?
Before I try to explain that, let's have a look how compilation would be
done by using C++ modules:
Let's assume we have the following class in a single module file:
class Test
{
public: void foo() {}
};
The C++ compiler has all the required information to generate
precompiled code and a precompiled header file, when the module is
imported for the first time shortly after compilation has started.
E.g. in another module:
#import "Test"
If the same module is used anywhere else in the same project the
compiler can (better said could) check if it already has compiled the
code and just use the precompiled header file (or code for inlining).
No need to reparse the whole module again - why should the compiler do
that anyways ? It has already compiled the code !!!
Now back to C++:
Why can't a C++0x compiler just do the same ?
The simple answer is because of the dumb preprocessor.
Every translation unit can use different macros and therefore the
preprocessor can emit different code.
The result is:
a) The compiler has to compile the same code over and over again
b) The C++ developer, to prevent too much code to be recompiled,
has to decouple the code manually.
But as soon as templates are involved you are lost in C++
and can't decouple appropriately anymore
GCC features precompiled headers for C++. I believe it solves
all mentioned problems. But what _I_ want is to remove private:
part from the header :-D Seriously now, a smart pointer field
can be used instead of a value for the same effect,
but having a value is a bit cleaner (although it brings more
headers to the game).
IMHO, a C++ developer does not decouple code manually to prevent
recompilation, but for the purposes of modularity, maintenance,
good design, etc. Templates do make a nice mess, however.
Modules should interact by the means of interfaces. I don't see
any problem with this and nothing to improve. In your case
it would be:
class Test {
public:
virtual ~Test() = 0;
virtual void foo() = 0;
};
This is the only thing that the user of Test "module" will have
to include. But if the module is made out of concrete classes
and there are too many headers involved:
http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html
One question - how do D modules get along with templates?
--
Dragan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]