Topic: operator...(void) Why not?


Author: henrik.quintel@gmx.de ("Henrik Quintel")
Date: Sun, 1 Dec 2002 18:41:35 +0000 (UTC)
Raw View
> What is wrong with
>
> printf("%s\n", s.c_str());
>
> Stephen Howe
In general. Why do you use printf and not the C++ I/O Streams?
printf comes from C99. Of course, you can use C99 but if possible,
you should use the mentioned I/O Streams.

Yours Henrik


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dickie@acm.org (Garth A. Dickie)
Date: Sun, 1 Dec 2002 18:42:22 +0000 (UTC)
Raw View
NOSPAMsjhowe@dial.pipex.com ("Stephen Howe") writes
> "VAdim" <vadima@waterint.com> writes
> > class String : public std::string {
> > public:
> >   const char * operator...(void){ return c_str(); }
> > };
> >
> > String s;
> >
> > printf("%s\n", s );
>
> What is wrong with
>
> printf("%s\n", s.c_str());

1. There are many situations where I would much rather read (C):

A) cout << a << " " << b << " " << c << " " << d << endl;
B) printf("%s %s %s %s\n", a.c_str(), b.c_str(), c.c_str(), d.c_str());
C) printf("%s %s %s %s\n", a, b, c, d);

2. The proposal also gives a way to make the following a compile-time error:

std::string s;
printf(%s", s);

Just declare operator ... private, with no definition.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: musiphil@bawi.org (KIM Seungbeom)
Date: Mon, 2 Dec 2002 07:05:55 +0000 (UTC)
Raw View
"Garth A. Dickie" wrote:
>
> NOSPAMsjhowe@dial.pipex.com ("Stephen Howe") writes
> >
> > What is wrong with
> >
> > printf("%s\n", s.c_str());
>
> 1. There are many situations where I would much rather read (C):
>
> A) cout << a << " " << b << " " << c << " " << d << endl;
> B) printf("%s %s %s %s\n", a.c_str(), b.c_str(), c.c_str(), d.c_str());
> C) printf("%s %s %s %s\n", a, b, c, d);

You can easily get into trouble if you want to mix the C-style stdio
and C++ objects. If you want to do it deliberately, extra care and
verboseness (adding .c_str()) are the prices you have to pay.

>
> 2. The proposal also gives a way to make the following a compile-time error:
>
> std::string s;
> printf(%s", s);
>
> Just declare operator ... private, with no definition.

Why do you think the "..." context of std::string occurs only in
printf? It is up to printf to require what it wants, and std::string
itself has no reason to convert to a const char* in "..." contexts.

--
KIM Seungbeom <musiphil@bawi.org>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Andreas.Wickner@sds.at (Andreas Wickner)
Date: Mon, 2 Dec 2002 16:50:55 +0000 (UTC)
Raw View
Garth A. Dickie wrote:
> 1. There are many situations where I would much rather read (C):
> A) cout << a << " " << b << " " << c << " " << d << endl;
> B) printf("%s %s %s %s\n", a.c_str(), b.c_str(), c.c_str(), d.c_str());
> C) printf("%s %s %s %s\n", a, b, c, d);

I would suggest to use boost::format for this:

   using namespace boost;
   cout << format("%s %s %s %s\n") % a % b % c % d;

Have a look at www.boost.org

Andreas Wickner

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: vadima@waterint.com (VAdim)
Date: Tue, 26 Nov 2002 22:42:13 +0000 (UTC)
Raw View
Hi Experts,

My proposal is to create operator...(void)
It will be called when object placed in the argument list for function
with elipses. Like

class String : public std::string {
public:
  const char * operator...(void){ return c_str(); }
};

String s;

printf("%s\n", s );

Smart pointers will also benefit from this proposal.

Some people prefer old good printf to streams...

Vadim.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: NOSPAMsjhowe@dial.pipex.com ("Stephen Howe")
Date: Wed, 27 Nov 2002 17:48:18 +0000 (UTC)
Raw View
"VAdim" <vadima@waterint.com> wrote in message
news:ac9acec7.0211261356.4521149d@posting.google.com...
> Hi Experts,
>
> My proposal is to create operator...(void)
> It will be called when object placed in the argument list for function
> with elipses. Like
>
> class String : public std::string {
> public:
>   const char * operator...(void){ return c_str(); }
> };
>
> String s;
>
> printf("%s\n", s );

What is wrong with

printf("%s\n", s.c_str());

Stephen Howe


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]