|
Boost : |
From: Kevin Atkinson (kevin_at_[hidden])
Date: 2004-02-06 16:39:53
Here is a very simple, but extremely useful, class I would like to submit
for inclusion for boost: parm_string.
parm_string is a special string class that is designed to be used as
a parameter for a function that is expecting a string. It will allow
either a "const char *" or "string" class to be passed in. It will
automatically convert to a "const char *". The string can also be
accesses via the "str" method. Usage example:
void foo(parm_string s1, parm_string s2) {
const char * str0 = s1;
size_t sz = s2.size()
if (s1 == s2 || s2 == "bar") {
...
}
}
..
string s1 = "...";
foo(s1);
const char * s2 = "...";
foo(s2);
By using parm_string instead on "const char *" one avoids the need
to have to use the c_str() member every time a string is passed
into a function. It is also more effect than just using "const
char *" as the length information is passed in if it is known.
It is also more efferent than just using "const string &" as that
will involve creating an unnecessary temporary when a non string
object is used.
In my experience most functions that require a string as a parameter
really only need the value of the string and possibly its length.
They don't need the functionally of the full "string" class.
Attached is parm_string.hpp and a simpile program parm_string.cpp
I use a variant of this class heavily in my programs I find it extremely
useful.
-- http://kevin.atkinson.dhs.org
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk