473,796 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3154
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_p tr<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

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

Similar topics

10
8185
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
2
3095
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 instances of std::string in that implementation. I did not find anything that
9
3302
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
3759
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) class library and type in the following code below: #include <map> #include<string>
9
3789
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 code. for example, this is bad practice: header.h #include <string> using std::string;
2
9229
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
2437
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 tried it on a few platforms (all with gcc unfortunaley) and its seems to be consistent. Adrian
11
4292
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. Yick. All I could come up with is: #include <string> int main() { std::string text;
12
12451
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 usually use GetBuffer for that LPTSTR CString::GetBuffer( int nMinBufLength )
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10190
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.