Re: [Dev-C++] #define a "function" with variable number of parameters
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2005-12-19 23:16:44
|
Probably a lot more non-gcc compilers than you think...
/Per W
On Mon, 19 Dec 2005, Josh Matthews wrote:
> What compiler are you using that doesn't allow syntax like:
>
> #define error(s,...) { fprintf(stderr,"ERROR %s:%d -
> "s"\n",__FILE__,__LINE__, ##__VA_ARGS__); fflush(stderr); }
> #define debug(s,...) { fprintf(stdout,s"\n",##__VA_ARGS__); }
>
> On 12/19/05, Minds Work <min...@gm...> wrote:
> >
> >
> > I know it is not a beatiful solution...
> > (but its the one tha will works as we want....)
> >
> >
> > On 12/19/05, Per Westermark <pw...@ia...> wrote:
> > >
> > > You do not want to use a solution where your source code contains
> > > mismatched parentheses. Syntax highlighting, and in some cases
> > > auto-indent, not to mention lint etc will dislike such code something
> > > grievously...
> > >
> > > /Per W
> > >
> > > On Mon, 19 Dec 2005, Minds Work wrote:
> > >
> > > > Hi Daneil,
> > > >
> > > > My goal is only to write less code, but i like your ideia, so I wil=
l
> > > use
> > > > something like this:
> > > > #define LOG_INFO_(log) if(log->isInfoEnabled()) log->(__FILE__,
> > > __LINE__,
> > > > #define LOG_INFO LOG_INFO_(LOG)
> > > >
> > > > #define LOG_DEBUG_(log) if(log->isDebugEnabled()) log->debug(__FILE=
__,
> > >
> > > > __LINE__,
> > > > #define LOG_DEBUG LOG_DEBUG_(LOG)
> > > >
> > > > And use as:
> > > >
> > > > LOG_DEBUG "x =3D %d", 2);
> > > >
> > > > LOG_INFO "%s", "this is a log message");
> > > >
> > > > PS: I already use a log library :)
> > > >
> > > >
> > > > On 12/18/05, Daniel K. O. <dan...@ya...> wrote:
> > > > >
> > > > > Per Westermark wrote:
> > > > >
> > > > > >Given the billions of lines of existing code without use of
> > > __VA_ARGS__,
> > > > > >it really isn't such an important function that it is worth it t=
o
> > > ignore
> > > > > >the current language standard.
> > > > > >
> > > > > >
> > > > > Agreed. I was just trying to answer the original question, but I
> > > also
> > > > > doubt that it's really needed.
> > > > > As Minds Work didn't specify what problem he's trying to solve, I
> > > assume
> > > > > that the example posted could be easily solved with Per's first
> > > sugestion.
> > > > >
> > > > > Something like this:
> > > > >
> > > > >
> > > > > -----
> > > > >
> > > > > #include <cstdarg>
> > > > > #include <cstdio>
> > > > >
> > > > > #define LOG log_prefix(__FILE__, __LINE__); log
> > > > >
> > > > > void log_prefix(const char *file, int line)
> > > > > {
> > > > > printf("%s:%d:\t", file, line);
> > > > > }
> > > > >
> > > > >
> > > > > // here is Per's code
> > > > > void log(const char *msg,...) {
> > > > > va_list ap;
> > > > >
> > > > > va_start(ap,msg);
> > > > > vprintf(msg,ap);
> > > > > va_end(ap);
> > > > > }
> > > > >
> > > > > int main() {
> > > > > LOG("test %u %u\n",1,2);
> > > > > return 0;
> > > > > }
> > > > >
> > > > > -----
> > > > >
> > > > >
> > > > > PS: There's also the C++ way to solve it, using ostreams and
> > > operator<<,
> > > > > that obviously won't need a variable number of parameters.
> > > > > PS2: if the problem is really just logging, there are tons of C++
> > > > > libraries that do just this - conveniently. Google a bit, maybe y=
ou
> > > can
> > > > > use one of them for your needs.
> > > > >
> > > > >
> > > > > ---
> > > > > Daniel K. O.
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > _______________________________________________________
> > > > > Yahoo! doce lar. Fa=E7a do Yahoo! sua homepage.
> > > > > http://br.yahoo.com/homepageset.html
> > > > >
> > > > >
> > > > >
> > > >
> > >
> > >
> >
>
>
> --
> Cheers,
> Josh
>
> PGP: http://revvy.box43.net/Josh_Matthews.asc
>
|