C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

User-defined manipulators that take arguments

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Dave
Guest





PostPosted: Sun Jan 23, 2005 3:02 am    Post subject: User-defined manipulators that take arguments Reply with quote




Hello all,

It is my understanding that there is not a portable way to write a
user-defined manipulator that takes arguments. Please consider below which,
for purposes of illustration, implements a manipulator similar to setw().
Presumably, I have broken a rule here and this program is non-conforming.
However, I cannot see what rule that would be. May I request those who are
so inclined to try and poke holes in it?

Thank!
Dave


#include <iostream>
#include <ostream>
#include <string>

using namespace std;

template<typename T, typename C>
class ManipInfra
{
public:
ManipInfra(
basic_ostream<C> &(*pFun)(basic_ostream<C> &, T),
T val
): manipFun_(pFun), val_(val)
{
}

void operator()(basic_ostream<C> &os) const
{
manipFun_(os, val_);
}

private:
T val_;
basic_ostream<C> &(*manipFun_)(basic_ostream<C> &, T);
};

template<typename T, typename C>
basic_ostream<C> &operator<<(
basic_ostream const ManipInfra<T, C> &manip
)
{
manip(os);

return os;
}

ostream &setTheWidth(ostream &os, int n)
{
os.width(n);

return os;
}

ManipInfra<int, char> setWidth(int n)
{
return ManipInfra<int, char>(setTheWidth, n);
}

int main()
{
cout << setWidth(10) << right << "foo" << endl;
}





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Sun Jan 23, 2005 10:15 am    Post subject: Re: User-defined manipulators that take arguments Reply with quote



In article <10v62e72h95jh56 (AT) news (DOT) supernews.com>, Dave
<better_cs_now (AT) yahoo (DOT) com> wrote:

Quote:
Hello all,

It is my understanding that there is not a portable way to write a
user-defined manipulator that takes arguments. Please consider below which,
for purposes of illustration, implements a manipulator similar to setw().
Presumably, I have broken a rule here and this program is non-conforming.
However, I cannot see what rule that would be. May I request those who are
so inclined to try and poke holes in it?

There are many portable ways to do this. The simplest is to write a
class whose constructor takes the arguments of your proposed
manipulator. and a member function say std::basic_ostream &
do_it(std::ostream &);

finally you want to overload operator << to 'output' your manimpulator
and a function to do template type deduction for your class. something
like the following is portable.

#include #include <ostream>

template <class T,class C,class CT>
class ManipulatorClass
{
T val_;
typedef std::basic_ostream<C,CT> stream_type;
typedef stream_type & (*func_type)(stream_type &,T);
func_type fun_;
public:
ManipulatorClass(func_type pFun,T val):fun_(pFun),val_(val){}
stream_type & do_it (stream_type &os) const
{
fun_(os,val);
return os;
}
};

template <class T,class C,class CT>
inline
ManipulatorClass<T,C,CT> Manipulator(std::basic_ostream<C,CT>(*pf)(T),T
val)
{
return ManipulationClass<T,C,CT> Manipulator(pf,val);
}

template <class T,class C,class CT>
inline
std::basic_ostream<C,CT> & operator << (std::basic_ostream const ManipulatorClass<T,C,CT> &manip)
{
return mainip.do_it(os);
}

// example usuage
std::cout << 5 << Manipulator(some_fun,20) << 26 << 'n';

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
dtmoore
Guest





PostPosted: Mon Jan 24, 2005 11:42 am    Post subject: Re: User-defined manipulators that take arguments Reply with quote




Dave wrote:
Quote:
Hello all,

It is my understanding that there is not a portable way to write a
user-defined manipulator that takes arguments. Please consider below
which,
for purposes of illustration, implements a manipulator similar to
setw().
Presumably, I have broken a rule here and this program is
non-conforming.
However, I cannot see what rule that would be. May I request those
who are
so inclined to try and poke holes in it?


What happens if I try to use your manip on an ostream where I have used
a non default char_traits? I am pretty sure that it will (at best)
generate a compile time error. However, it is (probably) easy to fix
that problem by adding another template parameter to your ManipInfra
class to represent the char_traits of the basic_ostream.

Dave Moore


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Jan 24, 2005 10:36 pm    Post subject: Re: User-defined manipulators that take arguments Reply with quote

Dave wrote:

Quote:
It is my understanding that there is not a portable way to
write a user-defined manipulator that takes arguments.

Why do you say that? Why wouldn't the implémentation of, say,
std::setw() be portable?

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dave
Guest





PostPosted: Tue Jan 25, 2005 10:47 am    Post subject: Re: User-defined manipulators that take arguments Reply with quote


Quote:
It is my understanding that there is not a portable way to
write a user-defined manipulator that takes arguments.

Why do you say that? Why wouldn't the implémentation of, say,
std::setw() be portable?

Josuttis, "The C++ Standard Library", page 613.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Tue Jan 25, 2005 7:59 pm    Post subject: Re: User-defined manipulators that take arguments Reply with quote

In article <10vbkn4ip9q6o05 (AT) news (DOT) supernews.com>, Dave
<better_cs_now (AT) yahoo (DOT) com> writes
Quote:
It is my understanding that there is not a portable way to
write a user-defined manipulator that takes arguments.

Why do you say that? Why wouldn't the implémentation of, say,
std::setw() be portable?

Josuttis, "The C++ Standard Library", page 613.

Where does Josuttis say that it is not possible to write portable
manipulators with parameters? AFAICS he does not touch on the subject.
If you want to know about them try 'Standard C++ IOStreams & Locales' by
Langer and Kreft (in particular, pages 179-185)



--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Michael Brooks
Guest





PostPosted: Wed Jan 26, 2005 7:14 pm    Post subject: Re: User-defined manipulators that take arguments Reply with quote

In article <0FmC$mVChm9BFwR2 (AT) robinton (DOT) demon.co.uk>, Francis Glassborow wrote:
Quote:
In article <10vbkn4ip9q6o05 (AT) news (DOT) supernews.com>, Dave
[email]better_cs_now (AT) yahoo (DOT) com[/email]> writes
Josuttis, "The C++ Standard Library", page 613.

Where does Josuttis say that it is not possible to write portable
manipulators with parameters?

Reading the last paragraph of that page in my copy (10th printing) gave
me that impression, too.

Quote:
AFAICS he does not touch on the subject.
If you want to know about them try 'Standard C++ IOStreams & Locales' by
Langer and Kreft (in particular, pages 179-185)

I don't have this text - my first attempt would be to try a functor with
operator() that could be called as required by the manipulator
interface, and takes the parameters in the constructor. Is that anything
like the way it should be done? If so, I don't see what is non-standard
- am I just reading Josuttis' argument wrongly?

--
Mick Brooks

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Thu Jan 27, 2005 9:19 pm    Post subject: Re: User-defined manipulators that take arguments Reply with quote

Michael Brooks wrote:

[concerning manipulators with parameters...]
Quote:
I don't have this text - my first attempt would be to try a
functor with operator() that could be called as required by
the manipulator interface, and takes the parameters in the
constructor.

It doesn't even have to be a functor. Any class for which you
define a << or a >> can do the trick. The constructor takes the
parameters and memorizes them, and in << or >>, you do whatever
you have to set the flags according to the parameters.

The standard defines the manipulators with parameters as
functions which return an unspecified type of object, whose <<
and >> operators did the trick; early version of CFront had some
macros, I think, to facilitate this. But personally, I don't
see what it buys compared to just constructing the object
explicitly. (Well, it does allow you to conform to a naming
convention which requires manipulators and functions to start
with small letters, and classes with caps. But I'd favor just
making an exception to the convention, and say that the names of
classes which act as manipulators should start with a small
letter.)

The function approach may be justified if some of the
manipulator's parameters are characters (say for fill); in this
case, the function allows for automatic type deduction, where as
using the class directly requires the user to explicitly specify
the character type.

Quote:
Is that anything like the way it should be done? If so, I
don't see what is non-standard - am I just reading Josuttis'
argument wrongly?

Without knowing what you think Josuttis was complaining about,
it's impossible to tell. As far as I know, the approach above
is fully compliant with the standard, and portable to all hosted
implementations.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Michael Brooks
Guest





PostPosted: Sat Jan 29, 2005 4:28 am    Post subject: Re: User-defined manipulators that take arguments Reply with quote

James Kanze wrote:

Hi James,

Quote:
Michael Brooks wrote:

[concerning manipulators with parameters...]
I don't have this text - my first attempt would be to try a
functor with operator() that could be called as required by
the manipulator interface, and takes the parameters in the
constructor.

It doesn't even have to be a functor. Any class for which you
define a << or a >> can do the trick. The constructor takes the
parameters and memorizes them, and in << or >>, you do whatever
you have to set the flags according to the parameters.

I have to admit that I haven't looked into this too closely - the
question about user defined manipulators with parameters not being
standard caught my attention, and I looked up the page reference in
Josuttis, and came to the same conclusion as the OP. Francis' assertion
that this was wrong left me confused.

Your explanation goes beyond the one in Josuttis, and I can't quite
follow it at the moment - but I won't ask more questions until I've
studied the problem better.

Quote:
Is that anything like the way it should be done? If so, I
don't see what is non-standard - am I just reading Josuttis'
argument wrongly?

Without knowing what you think Josuttis was complaining about,
it's impossible to tell.

Well, I'm not sure what Josuttis was complaining about, either, but he
seems pretty clear that there is a problem. A paragraph from section
13.6.1 - How Manipulators Work - has this:

"The C++ standard library also contains manipulators with arguments. How
these manipulators work exactly is implementation dependent, and there
is no standard way to implement user-defined manipulators with
arguments."

Quote:
As far as I know, the approach above is fully compliant with the
standard, and portable to all hosted implementations.

Thanks. I'll study it.

--
Mick Brooks
[email]michael.brooks (AT) physics (DOT) ox.ac.uk[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Jan 31, 2005 11:42 am    Post subject: Re: User-defined manipulators that take arguments Reply with quote

Michael Brooks wrote:

[...]
Quote:
Well, I'm not sure what Josuttis was complaining about,
either, but he seems pretty clear that there is a problem. A
paragraph from section 13.6.1 - How Manipulators Work - has
this:

"The C++ standard library also contains manipulators with
arguments. How these manipulators work exactly is
implementation dependent, and there is no standard way to
implement user-defined manipulators with arguments."

Which is perfectly correct, if you take the second use of
standard in its general sense. The C++ standard library
contains manipulators with arguments, but (as is the case with
most of the library) only defines the effects, not what the
implementation must do to achieve them. And there is no
standard way to do this -- different implementations use
different techniques. There are several standard conforming
ways to do this, but none has become the standard way.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.