string class not standard

D

djake

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
 
M

msalters

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
 
R

Rolf Magnus

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 said:
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.
 
M

Mike Wahler

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
 
A

abecedarian

Mike said:
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.
 
H

Howard

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
 
R

Rolf Magnus

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?
 
A

Abecedarian

Howard said:
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 ... ;-)
 
H

Howard

Abecedarian said:
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.
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".


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
 
M

Mike Wahler

Howard said:
#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
 
R

Rolf Magnus

Abecedarian said:
string;

BTW, this is a class template.


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.
 
A

Abecedarian

Rolf said:
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.
 
S

Samuel Krempp

(e-mail address removed) (03 May 2005 12:47,
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.
 
S

Samuel Krempp

(e-mail address removed) (03 May 2005 16:31,
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)
 
R

Rolf Magnus

Samuel said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top