Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

preprocessor when included

37 views
Skip to first unread message

mooingps...@gmail.com

unread,
Feb 6, 2015, 1:20:12 PM2/6/15
to
You know what feature I would love? A preprocessor statement that keeps a block of text unparsed until a certain other file is included. When that other file is included, the unparsed block of memory is inserted just after that file.

myclass.h

#include <string>
class myclass {
std::string name;
};

#when <iostream>
std::istream& operator>>(std::istream& in, myclass& data)
{return in>>data.name;}
std::ostream& operator<<(std::ostream& in, myclass& data)
{return in>>data.name;}
#endwhen

This would allow myclass.h to be parsed faster in cpp files that don't include iostream, and would avoid bringing in unnecessary headers. Not only would this have neat effects on compile times, it could also be used/abused to avoid bringing in unnecessary _link_ dependencies:

#when <bignetworking.h>
#pragma comment(lib,"bignetworking.lib")
inline HRESULT loadfromxml(myclass& data, const bigxmlblob& blob) {
//etc
}
inline HRESULT savetoxml(myclass& data, bigxmlblob& blob) {
//etc
}
#endwhen

red floyd

unread,
Feb 6, 2015, 9:54:46 PM2/6/15
to
Bad example. THat's what <iosfwd> is for.

Lőrinczy Zsigmond

unread,
Feb 11, 2015, 1:05:34 AM2/11/15
to
On 2015.02.06. 19:20, mooingps...@gmail.com wrote:
> You know what feature I would love? A preprocessor statement
> that keeps a block of text unparsed until a certain other file is
included.
> When that other file is included, the unparsed block of memory is
inserted
> just after that file.
>
> myclass.h
>
> #include <string>
> class myclass {
> std::string name;
> };
>
> #when <iostream>
> std::istream& operator>>(std::istream& in, myclass& data)
> {return in>>data.name;}
> std::ostream& operator<<(std::ostream& in, myclass& data)
> {return in>>data.name;}
> #endwhen

class std::istream;
class std::ostream;

std::istream& operator>>(std::istream& in, myclass& data);
std::ostream& operator<<(std::ostream& in, myclass& data);

/* implementation in file myclass.cc */

> #when <bignetworking.h>
> #pragma comment(lib,"bignetworking.lib")
> inline HRESULT loadfromxml(myclass& data, const bigxmlblob& blob) {
> //etc
> }
> inline HRESULT savetoxml(myclass& data, bigxmlblob& blob) {
> //etc
> }
> #endwhen

class bigxml;
HRESULT loadfromxml(myclass& data, const bigxmlblob& blob);
HRESULT savetoxml(myclass& data, bigxmlblob& blob);
/* implementation in file myclass.cc */

PS: please don't try to use the 'inline is fast' argument on me;)

Lőrinczy Zsigmond

unread,
Feb 11, 2015, 1:15:23 AM2/11/15
to
> > #when <bignetworking.h>
> > #pragma comment(lib,"bignetworking.lib")
> > inline HRESULT loadfromxml(myclass& data, const bigxmlblob& blob) {
> > //etc
> > }
> > inline HRESULT savetoxml(myclass& data, bigxmlblob& blob) {
> > //etc
> > }
> > #endwhen
>
> class bigxml;
> HRESULT loadfromxml(myclass& data, const bigxmlblob& blob);
> HRESULT savetoxml(myclass& data, bigxmlblob& blob);
> /* implementation in file myclass.cc */

I'd like to correct this:

class bigxmlblob;
HRESULT loadfromxml(myclass& data, const bigxmlblob& blob);
HRESULT savetoxml(myclass& data, bigxmlblob& blob);
/* implementation in file myclass_bigxmlblob.cc */

0 new messages