473,406 Members | 2,954 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.

Does anyone has already seen a non mutable String based on std::string

Hi,

I am currently implementing some basic classes from .NET into modern C++.
And I would like to know if someone would know a non mutable string class.
May 28 '06 #1
12 3118
Vincent RICHOMME posted:
Hi,

I am currently implementing some basic classes from .NET into modern
C++. And I would like to know if someone would know a non mutable
string class.

Never heard the term, "non-mutable class", before.
What's a non-mutable class? Is it a class which has no member objects which
are mutable?
-Tomás
May 28 '06 #2
Tomás a écrit :
Vincent RICHOMME posted:
Hi,

I am currently implementing some basic classes from .NET into modern
C++. And I would like to know if someone would know a non mutable
string class.

Never heard the term, "non-mutable class", before.
What's a non-mutable class? Is it a class which has no member objects which
are mutable?
-Tomás

non mutable means that once created object cannot be modidified.
In the case of string, it means that once the string has been allocated
it cannot be modified or if you want to modify it, the modification is
done on a copy and not on the string itself.
Actually I have found one implementation on sourceforge and based on
boost but I don't like boost dependencies.
May 28 '06 #3
Vincent RICHOMME posted:

non mutable means that once created object cannot be modidified.


#include <string>

int main()
{
std::string const str("Etched in stone.");
}
Am I missing something?
-Tomás
May 28 '06 #4
On Sun, 28 May 2006 16:59:28 +0200, Vincent RICHOMME
<ri******@free.fr> wrote:
Tomás a écrit :
Vincent RICHOMME posted:
I am currently implementing some basic classes from .NET into modern
C++. And I would like to know if someone would know a non mutable
string class.
Never heard the term, "non-mutable class", before.

What's a non-mutable class? Is it a class which has no member objects which
are mutable?

non mutable means that once created object cannot be modidified.
In the case of string, it means that once the string has been allocated
it cannot be modified or if you want to modify it, the modification is
done on a copy and not on the string itself.


Actually, you cannot translate an immutable Java or C# String class to
C++ without difficulties. A C++ string class is supposed to be a value
object (otherwise you would create yet another "smart" pointer)
whereas (immutable) strings in Java/C# are reference objects. In C++ a
truly immutable value object e.g. could not be copied into a
std::container.
Actually I have found one implementation on sourceforge and based on
boost but I don't like boost dependencies.


http://www.codeproject.com/string/fix_str.asp

I already have a newer version but since there is not much interest I
will not update article and code in the near future.

Best wishes,
Roland Pibinger
May 28 '06 #5
On Sun, 28 May 2006 15:10:09 GMT, I waved a wand and this message
magically appeared:
int main()
{
std::string const str("Etched in stone.");
}


D'you mean const std::string str("So's this") is different to that?

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 28 '06 #6
Alex Buell posted:
On Sun, 28 May 2006 15:10:09 GMT, I waved a wand and this message
magically appeared:
int main()
{
std::string const str("Etched in stone.");
}


D'you mean const std::string str("So's this") is different to that?

Haven't a clue why you suggested that. . . ?
The following two definitions are EXACTLY equivalent:

std::string const str;

const std::string str;
C++ just gives a bit of poetic license as to how you want to write it.
-Tomás
May 28 '06 #7
Vincent RICHOMME wrote:
I am currently implementing some basic classes from .NET into modern C++.
And I would like to know if someone would know a non mutable string class.


The fact that you appear to be unaware of the fundamental C++ concept
of "const" suggests to me that you are very likely to be porting into
anything one might reasonably recognize as "modern" C++. I do not mean
to disparage -- it's just that if you're lacking basic understanding of
the language you're porting to, you're setting yourself up for failure.
Read a good C++ book or three (see the FAQ for recommendations),
*then* try something like this. The time to get to a *good* solution,
including the time taken to read the books, will still be less than if
you don't.

Luke

May 28 '06 #8
Roland Pibinger wrote:
On Sun, 28 May 2006 16:59:28 +0200, Vincent RICHOMME
<ri******@free.fr> wrote:
Tomás a écrit :
Vincent RICHOMME posted:
I am currently implementing some basic classes from .NET into modern
C++. And I would like to know if someone would know a non mutable
string class.

Never heard the term, "non-mutable class", before.

What's a non-mutable class? Is it a class which has no member objects which
are mutable?

non mutable means that once created object cannot be modidified.
In the case of string, it means that once the string has been allocated
it cannot be modified or if you want to modify it, the modification is
done on a copy and not on the string itself.


Actually, you cannot translate an immutable Java or C# String class to
C++ without difficulties. A C++ string class is supposed to be a value
object (otherwise you would create yet another "smart" pointer)
whereas (immutable) strings in Java/C# are reference objects. In C++ a
truly immutable value object e.g. could not be copied into a
std::container.
Actually I have found one implementation on sourceforge and based on
boost but I don't like boost dependencies.


http://www.codeproject.com/string/fix_str.asp

I already have a newer version but since there is not much interest I
will not update article and code in the near future.


You might want to look at The most popular downloads in the Boost
Vault, where fixed_string was most popular download:

http://tinyurl.com/rstoa

Note that the version there was reviewed but not accepted:

http://www.boost.org/more/formal_review_schedule.html

The reasons were IIRC that the implementation had moved away from the
expected concept of a fixed string and so wasnt what potential users
were looking for. However the large amount of downloads suggests that
there is a lot of interest in this functionality. I havent looked at
your version in detail but from looking at the intro, it looks like it
might be worth posting a link to it on the boost developers list to see
whether there is interest. I think there might be.

regards
Andy Little

May 28 '06 #9
* Luke Meyers:
Vincent RICHOMME wrote:
I am currently implementing some basic classes from .NET into modern C++.
And I would like to know if someone would know a non mutable string class.


The fact that you appear to be unaware of the fundamental C++ concept
of "const" suggests to me that you are very likely to be porting into
anything one might reasonably recognize as "modern" C++. I do not mean
to disparage -- it's just that if you're lacking basic understanding of
the language you're porting to, you're setting yourself up for failure.
Read a good C++ book or three (see the FAQ for recommendations),
*then* try something like this. The time to get to a *good* solution,
including the time taken to read the books, will still be less than if
you don't.


An immutable string class is different from 'std::string const' in that
its operations do not carry the overhead associated with mutability, and
that it supports assignment and thus can be used in standard containers.

Functionality-wise it is similar to

typedef boost::shared_ptr<std::string const> ValueString;

Consider functions foo and bah,

extern void foo( std::string& s );
void bah( std::string s ) { foo( s ); }

Can the std::string implementation avoid full deep copying of the bah
argument, that is, copying the full string value? No, it can't in
general, because function foo might change the string content: at the
very latest the string value must be copied when foo does something that
/might/ change the string content, such as applying non-const
operator[]. However, consider

extern void foo( ValueString& s );
void bah( ValueString s ) { foo( s ); }

Can the ValueString implementation avoid deep copying the bah argument?

Not only can it avoid that, but with the typedef above it does avoid that.

So -- imaginary discussion with Luke ;-) -- why not then just write

void bah( std::string const& s ) { ... // Uh...

Yes, if you do that then you can't call function foo without first
making your own deep copy.

One problem with ValueString as defined above is that it has two levels
of dynamic memory management and allocation. Presumably a native
implementation of ValueString could manage with just one level,
dynamically allocating its internal buffer. Thus, more efficient.

There is also efficiency concerns with respect to thread safety. But my
mind is a little foggy right now. At least, the example generator is
completely silent, not coming up with any examples (I guess a bit of
Googling for string and thread safety and copy-on-write would be the thing).

So, it appears to me that Vincent is well aware of the C++ aspect.
--
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?
May 28 '06 #10
On 28 May 2006 10:57:50 -0700, "kwikius" >You might want to look at
The most popular downloads in the Boost
Vault, where fixed_string was most popular download:

http://tinyurl.com/rstoa

Note that the version there was reviewed but not accepted:

http://www.boost.org/more/formal_review_schedule.html
Grande confusione! boost::fixed_string is not an immutable string
class/template. The mentioned Boost library is boost::const_string<>
(which, of course, is not an accepted Boost library ;-).
The reasons were IIRC that the implementation had moved away from the
expected concept of a fixed string and so wasnt what potential users
were looking for. However the large amount of downloads suggests that
there is a lot of interest in this functionality. I havent looked at
your version in detail but from looking at the intro, it looks like it
might be worth posting a link to it on the boost developers list to see
whether there is interest. I think there might be.


Thank you for considering my code 'boost-able' but my goal was exactly
the opposite. 'fix_str' consists of very lightweight classes (not
templates, no traits and policies involved) with specified performance
characteristics for copying, assignment and default construction
(unlike std::string).

Best regards,
Roland Pibinger
May 28 '06 #11
On Sun, 28 May 2006 20:58:48 +0200, "Alf P. Steinbach"
<al***@start.no> wrote:
An immutable string class is different from 'std::string const' in that
its operations do not carry the overhead associated with mutability, and
that it supports assignment and thus can be used in standard containers.

Functionality-wise it is similar to

typedef boost::shared_ptr<std::string const> ValueString;


Actually, that should be avoided! Immutable objects make only sense as
immutable value objects. In C++ (not in Java) 'value object' means
that an assignment replaces (=mutates) the original object, not just a
reference to it. A totally immutable value object would not be very
useful in C++ (e.g. you could not assign a return value). I asked some
time ago for a name for 'assignable-but-otherwise-immutable-objects'.
The concept seems to be known but there is no established name for it.

http://groups.google.com/group/comp....1ef8b7634c0dc7

Best regards,
Roland Pibinger
May 28 '06 #12
Roland Pibinger wrote:
Thank you for considering my code 'boost-able' but my goal was exactly
the opposite.
The opposite of boost-able... ? Would that be unboost-able. Is this a
deliberate aim? :-)
'fix_str' consists of very lightweight classes (not
templates, no traits and policies involved) with specified performance
characteristics for copying, assignment and default construction
(unlike std::string).


AFAIK http://www.boost.org has no requirement that a candidate library
uses templates, traits or policies.

regrads
Andy Little

May 29 '06 #13

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

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Vyacheslav Kononenko | last post by:
All, If I am not mistaken I had some problems with code like this: std::string foo, bar; .... somefunc( foo.c_str(), bar.c_str() ); Problem was that c_str() used buffer shared btw...
9
by: Jim Langston | last post by:
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003
1
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003)...
9
by: Fei Liu | last post by:
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in...
2
by: suman.nandan | last post by:
Hi Experts, In the following code (sorry for using C printf in the code !) : ---------------------------------------------- #include <string> #include<cstdio> using namespace std; int main...
7
by: Adrian | last post by:
Why does std::strings find search from the begining of the string when pos >= (std::string::npos-3) I cant find anything in the standard that says what find should do if pos==npos in find I...
11
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr....
12
by: sas | last post by:
hi, i need that because the path functions for windows, like PathAppend and PathRemoveFileExt accept a writable zero terminated char*, but i didn't find that for std::string, with CString, i...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.