string class not standard

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • djake@excite.it

    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

  • msalters

    #2
    Re: string class not standard

    d...@excite.it wrote:[color=blue]
    > 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.[/color]

    Actually, it's std::string: "class string in namespace std"
    [color=blue]
    > Furthermore the class in the example is initialized with a
    > constructor like this
    >
    > string::string( );[/color]

    [color=blue]
    > 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?[/color]

    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

    Comment

    • abecedarian@spambob.com

      #3
      Re: string class not standard

      msalters wrote:[color=blue]
      > d...@excite.it wrote:[color=green]
      > > 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.[/color]
      >
      > Actually, it's std::string: "class string in namespace std"[/color]

      Standard C++ has no string class.

      Comment

      • Rolf Magnus

        #4
        Re: string class not standard

        djake@excite.it wrote:
        [color=blue]
        >
        > 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.[/color]

        Yes, it is. Defined in the <string> header in namespace std.
        [color=blue]
        > Furthermore the class in the example is initialized with a
        > constructor like this
        >
        > string::string( );[/color]

        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.
        [color=blue]
        > 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?[/color]

        Either you are using another than the standard string class or your standard
        library implementation is broken.
        [color=blue]
        > Why under unixware the string file contain a similar class with
        > invalid constructors?[/color]

        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.

        Comment

        • Mike Wahler

          #5
          Re: string class not standard


          <abecedarian@sp ambob.com> wrote in message
          news:1115051850 .839815.100250@ f14g2000cwb.goo glegroups.com.. .[color=blue]
          > msalters wrote:[color=green]
          >> d...@excite.it wrote:[color=darkred]
          >> > 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.[/color]
          >>
          >> Actually, it's std::string: "class string in namespace std"[/color]
          >
          > Standard C++ has no string class.[/color]

          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


          Comment

          • Markus Moll

            #6
            Re: string class not standard

            Hi

            abecedarian@spa mbob.com wrote:
            [color=blue]
            > Standard C++ has no string class.[/color]

            Not sure what you mean.

            Markus

            Comment

            • abecedarian@spambob.com

              #7
              Re: string class not standard

              Mike Wahler wrote:[color=blue]
              > <abecedarian@sp ambob.com> wrote in message[color=green]
              > > Standard C++ has no string class.[/color]
              >
              > Certainly it does (its standard library does). It's declared
              > (in namespace 'std') by standard header <string>.[/color]

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

              Comment

              • Howard

                #8
                Re: string class not standard


                <abecedarian@sp ambob.com> wrote in message
                news:1115058822 .083288.177890@ g14g2000cwa.goo glegroups.com.. .[color=blue]
                > Mike Wahler wrote:[color=green]
                >> <abecedarian@sp ambob.com> wrote in message[color=darkred]
                >> > Standard C++ has no string class.[/color]
                >>
                >> Certainly it does (its standard library does). It's declared
                >> (in namespace 'std') by standard header <string>.[/color]
                >
                > Look into the header <string>. I bet you don't find a string class
                > there.
                >[/color]

                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<ch ar, char_traits<cha r>, 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



                Comment

                • Rolf Magnus

                  #9
                  Re: string class not standard

                  abecedarian@spa mbob.com wrote:
                  [color=blue]
                  > Mike Wahler wrote:[color=green]
                  >> <abecedarian@sp ambob.com> wrote in message[color=darkred]
                  >> > Standard C++ has no string class.[/color]
                  >>
                  >> Certainly it does (its standard library does). It's declared
                  >> (in namespace 'std') by standard header <string>.[/color]
                  >
                  > Look into the header <string>. I bet you don't find a string class
                  > there.[/color]

                  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?

                  Comment

                  • Abecedarian

                    #10
                    Re: string class not standard

                    Howard wrote:[color=blue]
                    > <abecedarian@sp ambob.com> wrote in message
                    > news:1115058822 .083288.177890@ g14g2000cwa.goo glegroups.com.. .[color=green]
                    > > Mike Wahler wrote:[color=darkred]
                    > >> <abecedarian@sp ambob.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>.[/color]
                    > >
                    > > Look into the header <string>. I bet you don't find a string class
                    > > there.[/color]
                    >
                    > The class std::string is most certainly part of the standard C++[/color]
                    language.[color=blue]
                    > It's a template class. It's defined as:
                    >
                    > typedef basic_string<ch ar, char_traits<cha r>, allocator<char> >[/color]
                    string;

                    BTW, this is a class template.
                    [color=blue]
                    > Just because it's a template class, doesn't mean it's not a class![/color]

                    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".
                    [color=blue]
                    > Check the standard if you don't believe it's part of the language.[/color]

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

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

                    Comment

                    • Howard

                      #11
                      Re: string class not standard


                      "Abecedaria n" <abecedarian@sp ambob.com> wrote in message
                      news:1115066462 .468273.155130@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                      > Howard wrote:[color=green]
                      >> <abecedarian@sp ambob.com> wrote in message
                      >> news:1115058822 .083288.177890@ g14g2000cwa.goo glegroups.com.. .[color=darkred]
                      >> > Mike Wahler wrote:
                      >> >> <abecedarian@sp ambob.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.[/color]
                      >>
                      >> The class std::string is most certainly part of the standard C++[/color]
                      > language.[color=green]
                      >> It's a template class. It's defined as:
                      >>
                      >> typedef basic_string<ch ar, char_traits<cha r>, allocator<char> >[/color]
                      > string;
                      >
                      > BTW, this is a class template.[/color]

                      Well, if you want to get technical, the above is just a typedef... an alias
                      to a template, in this case.
                      [color=blue]
                      >[color=green]
                      >> Just because it's a template class, doesn't mean it's not a class![/color]
                      >
                      > 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".
                      >[color=green]
                      >> Check the standard if you don't believe it's part of the language.[/color]
                      >
                      > But you just confirmed that there is no string class just a clumsy
                      > basic_string<ch ar, char_traits<cha r>, allocator<char> > class template.
                      >[/color]

                      What's clumsy about it? Perhaps you're just not comfortable with templates?
                      [color=blue]
                      > Don't shoot the messenger ... ;-)[/color]

                      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("somethi ng"); // 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











                      Comment

                      • Mike Wahler

                        #12
                        Re: string class not standard


                        "Howard" <alicebt@hotmai l.com> wrote in message
                        news:zsxde.1731 93$cg1.120473@b gtnsc04-news.ops.worldn et.att.net...[color=blue]
                        >
                        > #include <string>
                        > using std::string;
                        >
                        > int main()
                        > {
                        > string strA(); // empty string[/color]

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

                        Gotcha! :-)

                        -Mike


                        Comment

                        • Mike Wahler

                          #13
                          Re: string class not standard


                          "Markus Moll" <moll@rbg.infor matik.tu-darmstadt.de> wrote in message
                          news:42765d07$0 $7516$9b4e6d93@ newsread2.arcor-online.net...[color=blue]
                          > Hi
                          >
                          > abecedarian@spa mbob.com wrote:
                          >[color=green]
                          >> Standard C++ has no string class.[/color]
                          >
                          > Not sure what you mean.[/color]

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

                          -Mike


                          Comment

                          • Rolf Magnus

                            #14
                            Re: string class not standard

                            Abecedarian wrote:
                            [color=blue][color=green]
                            >> 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<ch ar, char_traits<cha r>, allocator<char> >[/color]
                            > string;
                            >
                            > BTW, this is a class template.
                            >[color=green]
                            >> Just because it's a template class, doesn't mean it's not a class![/color]
                            >
                            > It's not a class, it's a template with 3 template parameters.[/color]

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

                            Comment

                            • Abecedarian

                              #15
                              Re: string class not standard

                              Rolf Magnus wrote:[color=blue]
                              > std::basic_stri ng is. std::string is not. The former is a class[/color]
                              template,[color=blue]
                              > the latter an instance of it, i.e. a class.[/color]

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

                              Comment

                              Working...