469,926 Members | 1,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

string class not standard


In the stroustrup C++ programming language (third edition) i can
find example like this:

string s1= "Hello";

So I imagine string is a standard class.

Furthermore the class in the example is initialized with a
constructor like this

string::string();

The strange thing is this:

Under unixWare i've string class declared only with this
constructor:

string::string(const char* c);

This doesn't seem standard solution! How is it possible?

Why under unixware the string file contain a similar class with
invalid constructors?

Thanks in advance

Jul 23 '05 #1
27 2092
d...@excite.it wrote:
In the stroustrup C++ programming language (third edition) i can
find example like this:

string s1= "Hello";

So I imagine string is a standard class.
Actually, it's std::string: "class string in namespace std"
Furthermore the class in the example is initialized with a
constructor like this

string::string();
The strange thing is this:

Under unixWare i've string class declared only with this
constructor:

string::string(const char* c);

This doesn't seem standard solution! How is it possible?


Because it's not the standard std::string, but a string in another
namespace? Because it's declared earlier as
string::string(const char* c = 0); and the implementation has a
special case for c==0? Because you missed the other declarations
(e.g, the copy ctor seems to be missing as well)

Regards,
Michiel Salters

Jul 23 '05 #2
msalters wrote:
d...@excite.it wrote:
In the stroustrup C++ programming language (third edition) i can
find example like this:

string s1= "Hello";

So I imagine string is a standard class.


Actually, it's std::string: "class string in namespace std"


Standard C++ has no string class.

Jul 23 '05 #3
dj***@excite.it wrote:

In the stroustrup C++ programming language (third edition) i can
find example like this:

string s1= "Hello";

So I imagine string is a standard class.
Yes, it is. Defined in the <string> header in namespace std.
Furthermore the class in the example is initialized with a
constructor like this

string::string();
No, it isn't. First, a temporary string is created using a conversion
constructor from const char*, then s1 is copy-constructed from that. The
compiler can omit the temporary and use the conversion constructor to
construct s1 directly.
The strange thing is this:

Under unixWare i've string class declared only with this
constructor:

string::string(const char* c);

This doesn't seem standard solution! How is it possible?
Either you are using another than the standard string class or your standard
library implementation is broken.
Why under unixware the string file contain a similar class with
invalid constructors?


Show a minimal compilable program that fails on that compiler, together with
the error message generated by it. This way, we can make sure whether the
error is the compiler's or yours.

Jul 23 '05 #4

<ab*********@spambob.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
msalters wrote:
d...@excite.it wrote:
> In the stroustrup C++ programming language (third edition) i can
> find example like this:
>
> string s1= "Hello";
>
> So I imagine string is a standard class.


Actually, it's std::string: "class string in namespace std"


Standard C++ has no string class.


Certainly it does (its standard library does). It's declared
(in namespace 'std') by standard header <string>.

/* completely standard code: */
#include <string>

int main()
{
std::string s("Hello");
return 0;
}

-Mike
Jul 23 '05 #5
Hi

ab*********@spambob.com wrote:
Standard C++ has no string class.


Not sure what you mean.

Markus

Jul 23 '05 #6
Mike Wahler wrote:
<ab*********@spambob.com> wrote in message
Standard C++ has no string class.


Certainly it does (its standard library does). It's declared
(in namespace 'std') by standard header <string>.


Look into the header <string>. I bet you don't find a string class
there.

Jul 23 '05 #7

<ab*********@spambob.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Mike Wahler wrote:
<ab*********@spambob.com> wrote in message
> Standard C++ has no string class.


Certainly it does (its standard library does). It's declared
(in namespace 'std') by standard header <string>.


Look into the header <string>. I bet you don't find a string class
there.


The class std::string is most certainly part of the standard C++ language.
It's a template class. It's defined as:

typedef basic_string<char, char_traits<char>, allocator<char> > string;

Just because it's a template class, doesn't mean it's not a class!

Check the standard if you don't believe it's part of the language.

-Howard

Jul 23 '05 #8
ab*********@spambob.com wrote:
Mike Wahler wrote:
<ab*********@spambob.com> wrote in message
> Standard C++ has no string class.


Certainly it does (its standard library does). It's declared
(in namespace 'std') by standard header <string>.


Look into the header <string>. I bet you don't find a string class
there.


I looked, and I found one. Well, ok, it isn't in <string> itself in the
standard library implementation I use, but in one that is #included by
<string>, but that doesn't really make a difference.
So what now?

Jul 23 '05 #9
Howard wrote:
<ab*********@spambob.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Mike Wahler wrote:
<ab*********@spambob.com> wrote in message
> Standard C++ has no string class.

Certainly it does (its standard library does). It's declared
(in namespace 'std') by standard header <string>.
Look into the header <string>. I bet you don't find a string class
there.


The class std::string is most certainly part of the standard C++

language. It's a template class. It's defined as:

typedef basic_string<char, char_traits<char>, allocator<char> > string;

BTW, this is a class template.
Just because it's a template class, doesn't mean it's not a class!
It's not a class, it's a template with 3 template parameters. If it
were a class usability would be much better. Just look at the compiler
error messages you get using the so called "string class".
Check the standard if you don't believe it's part of the language.


But you just confirmed that there is no string class just a clumsy
basic_string<char, char_traits<char>, allocator<char> > class template.

Don't shoot the messenger ... ;-)

Jul 23 '05 #10

"Abecedarian" <ab*********@spambob.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Howard wrote:
<ab*********@spambob.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
> Mike Wahler wrote:
>> <ab*********@spambob.com> wrote in message
>> > Standard C++ has no string class.
>>
>> Certainly it does (its standard library does). It's declared
>> (in namespace 'std') by standard header <string>.
>
> Look into the header <string>. I bet you don't find a string class
> there.
The class std::string is most certainly part of the standard C++

language.
It's a template class. It's defined as:

typedef basic_string<char, char_traits<char>, allocator<char> >

string;

BTW, this is a class template.


Well, if you want to get technical, the above is just a typedef... an alias
to a template, in this case.
Just because it's a template class, doesn't mean it's not a class!
It's not a class, it's a template with 3 template parameters. If it
were a class usability would be much better. Just look at the compiler
error messages you get using the so called "string class".
Check the standard if you don't believe it's part of the language.


But you just confirmed that there is no string class just a clumsy
basic_string<char, char_traits<char>, allocator<char> > class template.


What's clumsy about it? Perhaps you're just not comfortable with templates?
Don't shoot the messenger ... ;-)


I always shoot the messenger. Even for good news. I just like doing things
like that. :-O

But, I fail to see your point. You stated that "Standard C++ has no string
class". If all you are saying is that the class std::string is actually a
template, then so what? It's still part of the standard, and you still
create an instance of it just like any other class:

#include <string>
using std::string;

int main()
{
string strA(); // empty string
string strB = "something"; // copy of "something"
string* pstrA = new string("something"); // pointer to string containing
"something"
delete pstrA;
}

Your statement that C++ has no such class implies that "string" is not part
of standard C++, and that the OP ought not rely on it. But that's simply
wrong, if that was your intent. If your statement was simply that
std::string is technically a typedef for a template, then you might have
stated that explicitly, instead of simply declaring there was no such class.
(But in that case, why bother making the statement at all?)

-Howard



Jul 23 '05 #11

"Howard" <al*****@hotmail.com> wrote in message
news:zs*********************@bgtnsc04-news.ops.worldnet.att.net...

#include <string>
using std::string;

int main()
{
string strA(); // empty string


Not a string at all. A declaration of a function named
'strA()' which returns type 'string'.

Gotcha! :-)

-Mike
Jul 23 '05 #12

"Markus Moll" <mo**@rbg.informatik.tu-darmstadt.de> wrote in message
news:42**********************@newsread2.arcor-online.net...
Hi

ab*********@spambob.com wrote:
Standard C++ has no string class.


Not sure what you mean.


He means that he doesn't consider a class template
specialization to be a type. Why, I don't know.

-Mike
Jul 23 '05 #13
Abecedarian wrote:
The class std::string is most certainly part of the standard C++
language.
It's a template class. It's defined as:

typedef basic_string<char, char_traits<char>, allocator<char> >

string;

BTW, this is a class template.
Just because it's a template class, doesn't mean it's not a class!


It's not a class, it's a template with 3 template parameters.


std::basic_string is. std::string is not. The former is a class template,
the latter an instance of it, i.e. a class.

Jul 23 '05 #14
Rolf Magnus wrote:
std::basic_string is. std::string is not. The former is a class template, the latter an instance of it, i.e. a class.


class template, class template instantiation, class: 3 different
concepts in C++. Mix them and you create confusion.

Jul 23 '05 #15

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:_3*****************@newsread2.news.pas.earthl ink.net...

"Howard" <al*****@hotmail.com> wrote in message
news:zs*********************@bgtnsc04-news.ops.worldnet.att.net...

#include <string>
using std::string;

int main()
{
string strA(); // empty string


Not a string at all. A declaration of a function named
'strA()' which returns type 'string'.

Gotcha! :-)

-Mike


Touché! :-)

-Howard
Jul 23 '05 #16
ab*********@spambob.com (03 May 2005 12:47,
<11**********************@o13g2000cwo.googlegroups .com>) a écrit :
Rolf Magnus wrote:
std::basic_string is. std::string is not. The former is a class

template,
the latter an instance of it, i.e. a class.


class template, class template instantiation, class: 3 different
concepts in C++. Mix them and you create confusion.


they are obviously linked one with each other.
An instantiated class is a kind of class. It's a class that is
'generated' (proper term being instantiated) from a class template.
What confusion is there ? I find that crystal clear.

Some rules about templates can be complex, for instance you can come up with
very clever designs based on partial specializations.

But that does not put any confusion on the *use* of instantiated classes.

The error message might be longer, but a compiler is free to report error
about 'std::string' rather than
'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' ,
if it were judged a benefit, and important enough, by the compiler vendor.

That's hardly an issue with the use of class templates in the standard
library.

--
Samuel
Jul 23 '05 #17

Gotcha! :-)

-Mike


Touchi! :-)

-Howard


Aarrgh...that was an accented e when I sent it..not it's an i. I hate
Outlook Express!
Jul 23 '05 #18
al*****@hotmail.com (03 May 2005 16:31,
<6X*********************@bgtnsc05-news.ops.worldnet.att.net>) a écrit :

Gotcha! :-)

-Mike
Touché! :-)


Aarrgh...that was an accented e when I sent it..not it's an i. I hate
Outlook Express!


if it's any comfort to you : it did show up as 'é' for me :)

(I think your message did not have a 'Content-Type' field, so newsreaders
assume default encoding. I expect many would assume latin-1)

--
Samuel
Jul 23 '05 #19
Samuel Krempp wrote:
Gotcha! :-)

-Mike

Touché! :-)

Aarrgh...that was an accented e when I sent it..not it's an i. I hate
Outlook Express!


if it's any comfort to you : it did show up as 'é' for me :)


For me, it didn't show up in his posting at all. It did show up in your
answer though, since your posting has a proper Content-Type field.
(I think your message did not have a 'Content-Type' field,
Yes, that field is missing in Howard's posting. But I think if the charset
is explicitly set in one of the configuration dialogs, even Outlook Express
is able to generate one. It just doesn't by default, which is rather
stupid.
so newsreaders assume default encoding. I expect many would assume
latin-1)


I have set mine to a default of utf-8.

Jul 23 '05 #20
ra******@t-online.de (03 May 2005 17:33,
<d5*************@news.t-online.com>) a écrit :
so newsreaders assume default encoding. I expect many would assume
latin-1)


I have set mine to a default of utf-8.


right, that's going to be more and more of a common default.
But aren't there still a few forums with policies against posting in UTF-8 ?

Also that explains why the 'é' was invisible for you : within ASCII text, it
makes the sequence of bytes invalid for UTF-8, so it caused an error when
reading the message, and the erroneus byte was ignored.

--
Samuel
PS : what's that "GNU Outlook Express 0.00.0000.0002" you're using ?
8)
Jul 23 '05 #21

ab*********@spambob.com schreef:
msalters wrote:
d...@excite.it wrote:
In the stroustrup C++ programming language (third edition) i can
find example like this:

string s1= "Hello";

So I imagine string is a standard class.


Actually, it's std::string: "class string in namespace std"


Standard C++ has no string class.


It has two.
See ISO 14882, section 21.2 String classes [lib.string.classes]

HTH,
Michiel Salters

Jul 23 '05 #22
msalters wrote:
ab*********@spambob.com schreef:
Standard C++ has no string class.


It has two.
See ISO 14882, section 21.2 String classes [lib.string.classes]


FYI, I was persuaded that there is almost no difference between a class
template instantiation and a 'real' class (i.e. a class without
template parameters) and that both constitute a class in C++.

::A::

Jul 23 '05 #23
Abecedarian wrote:
Rolf Magnus wrote:
std::basic_string is. std::string is not. The former is a class

template, the latter an instance of it, i.e. a class.

class template, class template instantiation, class: 3 different
concepts in C++.


I fail to see how a class template instantiation is not a class.
Jul 23 '05 #24
Maybe the information in my question was incomplete, so i want
include the entire header file I found in unixware systems. It doesn't
seem standard to me. And this doesn't permit to declar a string in this
way:

string varStr;

Infact the previous line generate error using my CC compiler.

Below the integral content of /usr/include/CC/string:

#ident "@(#)unixsrc:usr/src/common/lib/libC/string /main/uw7_nj/1"

// this is a dummy version of the new standard library <string> class,
// intended only to allow some of the new exception classes to work

#ifdef __cplusplus

#ifndef _STRING_CXX
#define _STRING_CXX

class string {
public:
string(const char* c) { s = c; }
const char* c_str() const { return s; }
private:
const char* s;
};

#endif

#endif

Jul 23 '05 #25
dj***@excite.it wrote:
Maybe the information in my question was incomplete, so i want
include the entire header file I found in unixware systems. It doesn't
seem standard to me.
It isn't.
And this doesn't permit to declar a string in this way:

string varStr;
Right.
// this is a dummy version of the new standard library <string> class,
// intended only to allow some of the new exception classes to work


Well, doesn't that comment give you a hint?

Jul 23 '05 #26
> I fail to see how a class template instantiation is not a class.

See how far this gets you:

namespace std {
class string;
}

#include <string>

Jul 23 '05 #27
friedlingu...@yahoo.com wrote:
I fail to see how a class template instantiation is not a class.


See how far this gets you:

namespace std {
class string;
}

#include <string>


Hey, you're going to be bashed ... ;-)

Jul 23 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Angus Leeming | last post: by
16 posts views Thread by Steve | last post: by
23 posts views Thread by YinTat | last post: by
19 posts views Thread by Mike Tyka | last post: by
10 posts views Thread by cmk128 | last post: by
87 posts views Thread by Robert Seacord | last post: by
5 posts views Thread by AMP | last post: by
10 posts views Thread by lovecreatesbea... | last post: by
5 posts views Thread by Ray | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.