public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Maximilian Schneider <king@greatfreeworld.org>
To: richardcavell@mail.com
Cc: gcc-help@gcc.gnu.org
Subject: Re: How to best cope with variadic input
Date: Tue, 01 Mar 2011 11:33:00 -0000	[thread overview]
Message-ID: <1298979185.2522.4.camel@slaptop> (raw)
In-Reply-To: <AANLkTin15K1ckkM_imOEg5bVahRCb+Qg-EXsXCd37PtN@mail.gmail.com>

On Tue, 2011-03-01 at 08:59 +0000, Jonathan Wakely wrote:
> On 1 March 2011 05:11,  <richardcavell@mail.com> wrote:
> > Hi everyone.  I've decided to learn C99, and so I'm passing -std=c99 to gcc.
> >  I've decided to write a Wikipedia bot in C (don't laugh).
> >
> > Owing partly to obsessive tendencies and partly due to the need for the bot
> > to be robust in working unattended at high speed, I want to wrap printf in a
> > wrapper that automatically detects when printf has failed and tells the
> > user.  The idea is that it is called like this:
> >
> > int res = safeprint ( __FILE__ , __LINE__ , "Blah" ) ;
> > if ( res < 0 ) // uh oh
> >
> >  But printf has variadic input.  I am scratching my head wondering what is
> > my best option:
> >
> > 1.  Make safeprint a variadic function  (I don't know how to do this)
> 
> Use va_list, va_start, va_arg and va_end, defined by:
> #include <stdarg.h>
> 
> On GNU/Linux you should be able to say "man stdarg.h" to see usage examples.
> 
> > 2.  Turn on C++ and overload safeprint with different combinations of
> > arguments
> 
> A better option would be to use a C++ variadic template.
> 
> > 3.  Create multiple safeprint functions with different names, that take
> > different combinations of arguments
> 
> That would be inconvenient to use.

I would have to agree with Jonathan here. In my experience with c++ (Qt)
overloading has only caused me trouble.

the va_list method is straight forward. Note that printf() even comes in
a va_list compatible form. vprintf()

Max S.

  reply	other threads:[~2011-03-01 11:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-01  5:12 richardcavell
2011-03-01  8:59 ` Jonathan Wakely
2011-03-01 11:33   ` Maximilian Schneider [this message]
2011-03-01 21:41     ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1298979185.2522.4.camel@slaptop \
    --to=king@greatfreeworld.org \
    --cc=gcc-help@gcc.gnu.org \
    --cc=richardcavell@mail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).