Topic: Where positional parameters in iostreams (aka printf) considered for Standard C++ Library?
Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/10/21 Raw View
"Matthew Blakley" <zarcher@nospam.yahoo.com> writes:
> I'm curious as to what, if anything, was considered for providing positional
> parameters in iostreams for the Standard C++ library.
I would assume nothing. You can roll your own, though.
> For example, something like the following:
>
> using namespace std;
> cout << form("<1> went to the <2> today.")
> << pos(1) << "Bob"
> << pos(2) << "store"
> << endf;
Here's a simple version. It doesn't handle manipulators or
more than 10 parameters or errors.
class pos_ostream
{
string fmt;
ostream ⌖
vector<string> args;
public:
pos_ostream(string f, ostream &o = cout) : fmt(f), target(o) { }
template<typename T> pos_ostream &operator<<(const T &item)
{
ostringstream s;
s << item;
args.push_back(s.str());
return *this;
}
~pos_ostream()
{
for (int i = 0; i < fmt.size(); ++i)
{
if (fmt[i] == '%')
target << args[fmt[++i] - 1];
else
target << fmt[i];
}
}
};
pos_ostream("%1 went to the %2 today.") << "Bob" << "store";
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/10/18 Raw View
On 17 Oct 99 10:21:41 GMT, Matthew Blakley <zarcher@nospam.yahoo.com> wrote:
> cout << form("<1> went to the <2> today.")
> << pos(1) << "Bob"
> << pos(2) << "store"
> << endf;
>Is anything of this nature being considered for future revisions of the
>standard?
This will just make extra overhead, because operator<< may now do one
of two things -- either write to the stream or fill in a positional
parameter.
What about type safety? Must "<1>" be a string or int or what? If
it is always a string, then use
printf("%s went to the %s today." ,"Bob", "store")
If "<1>" can be anything, then we are back to regular old printf
with all its problems -- lack of compile time type safety, limited
to only the fundamental types, no exception safety.
Also consider writing your own manipulators
class Went {
public:
Went(char const * who, char const * where) : who(who), where(where) { }
std::ostream& friend operator<<(std::ostream&, const Went&);
private:
char const *const who;
char const *const where;
};
std::ostream& operator<<(std::ostream& ostream, const Went& went) {
return ostream << went.who << " to the " << went.where " << " today.";
}
--
--------------
siemel b naran
--------------
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]