473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

User-defined manipulators that accept arguments


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<C> &os,
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;
}

Jul 22 '05 #1
2 2144
* Dave:

It is my understanding that there is not a portable way to write a
user-defined manipulator that takes arguments.
Where on Earth did you get that idea?

But it does seem to be difficult and perhaps impossible to provide
a solution that is both efficient and totally type-safe for arbitrary
argument type.

That is a general problem in C++.

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.
Yes, you use three spaces for indentation. That was just something I
suggested tongue-in-cheek in a meeting. And what do you know, suddenly
they all started using it, and now, years after, there seems to be no
way to stop 'em... :-(

Seriously, you have _inconsistent_ indentation.

That's Very Bad, although not a language rule violation.
[snip] May I request those who are so inclined to try and poke holes in it?
OK.

#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)
{
}
Use typedefs.

You don't support non-copyable T objects.

For a generic ManipInfra class don't copy the T objects: pass by
reference to const, all the way (that means that the final iostream
manipulator function cannot be used as a standalone function, add a
comment to that effect).

void operator()(basic_ostream<C> &os) const
{
manipFun_(os, val_);
}
To support the client code return the stream object.
private:
T val_;
basic_ostream<C> &(*manipFun_)(basic_ostream<C> &, T);
};

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

return os;
}


E.g. this would be simplified.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

"Alf P. Steinbach" <al***@start.no> wrote in message
news:41****************@news.individual.net...
* Dave:

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


Where on Earth did you get that idea?


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

Thanks for the reply; I'll study it more tomorrow when my wits are about me!
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
9
by: Gordon | last post by:
I want to add a feature to a project I'm working on where i have multiple users set up on my Postgres database with varying levels of access. At the bare minimum there will be a login user who...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.