Q for Roberto et al. Are #includes evil?

Steven T. Hatton hattons at globalsymmetry.com
Fri Sep 10 10:35:06 UTC 2004


On Wednesday 08 September 2004 03:33, Roberto Raggi wrote:
> Hi Steven,
>
> Q for Roberto et al. Are #includes evil?
> A: YES!!
>
> the preprocessor (not only the #include directive) is evil! Well at least
> for applications like KDevelop, but the preprocessor is very useful for
> many other applications. I mean the preprocessor is a Tool and as all the
> other tools it has PRO and CONS.

I know Stroustrup doesn't care for the CPP.  I try to write my code with a few 
macros as I can.  The only place I use the CPP is for #include and with Qt 
(because Qt requires it).  I often wonder if the things the CPP are used for 
could not be accomplished using internal language features such as templates. 
I really don't know what code would look like if people simply forced 
themselves to use native C++ rather than the CPP.  Take, for example the 
following macro from lib/cppparser/parser.cpp

#define UPDATE_POS(node, start, end) \
{ \
   int line, col; \
   const Token &a = lex->tokenAt(start); \
   const Token &b = lex->tokenAt( end!=start ? end-1 : end ); \
   a.getStartPosition( &line, &col ); \
   (node)->setStartPosition( line, col ); \
   b.getEndPosition( &line, &col ); \
   (node)->setEndPosition( line, col ); \
   if( (node)->nodeType() == NodeType_Generic ) { \
       if ((start) == (end) || (end) == (start)+1) \
           (node)->setSlice(lex->source(), a.position(), a.length()); \
       else \
           (node)->setText( toString((start),(end)) ); \
   } \
}

Could a template be used as effectively to accomplish the same thing?

There are some things the CPP is used for that would be very difficult to 
accomplish with existing C++ facilities.  One of them is duplicating a string 
as both source code, and a literal string.  For example, if you want to have 
as a class member, a string holding the name of your class.  You can't do 
that with templates.  It might be nice if there were a way to pass a string 
to a template so that it could be used like that. 

> I'm sure you'll find these papers interesting
> https://netfiles.uiuc.edu/garrido/www/publications.html

Yes, they are interesting.  Thanks.

-- 
Regards,
Steven




More information about the KDevelop-devel mailing list