using std::string; string("hello") vs std::string("hello") in header file.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fei Liu

    using std::string; string("hello") vs std::string("hello") in header file.

    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;
    class a{
    string x;
    };

    instead, one should write:
    #include <string>
    class a{
    std::string x;
    };

    The reason given by the author is that 'using std::string' actually pollutes
    the scope where 'using std::string' is declared.
    I find this style is rather inconvenient when I want to experiment with
    different implementation of certain classes in large projects. For example,
    I may want to try myns::string or john::string, it's actually much easier to
    replace 'using std::string' to 'using myns::string' and instantly switch to
    another string implementation. This is much better than going through all
    the instances of 'std::string variable' and replace them to 'myns::string
    variable'.

    What are your thoughts on this coding style?


  • Phlip

    #2
    Re: using std::string; string(&quot;he llo&quot;) vs std::string(&qu ot;hello&quot;) in header file.

    Fei Liu wrote:
    [color=blue]
    > 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.[/color]

    Doesn't it say "at top level"?
    [color=blue]
    > The reason given by the author is that 'using std::string' actually
    > pollutes
    > the scope where 'using std::string' is declared.
    > I find this style is rather inconvenient when I want to experiment with
    > different implementation of certain classes in large projects. For
    > example,
    > I may want to try myns::string or john::string, it's actually much easier
    > to
    > replace 'using std::string' to 'using myns::string' and instantly switch
    > to
    > another string implementation. This is much better than going through all
    > the instances of 'std::string variable' and replace them to 'myns::string
    > variable'.
    >
    > What are your thoughts on this coding style?[/color]

    In general, write a friggin "using std::string;" wherever you like, in fresh
    code where nothing is called "string" except the std:: class.

    The guideline is to never put anything in a .h file that could cause trouble
    in any .cpp file that includes it. This rule implies the area outside a
    class{} or namespace{} block should be reserved for things that absolutely
    must go there, and convenient things like typedef or using are more trouble
    than this minor convenience.

    In your case, you could write this in a header:

    namespace zone {
    // using john::string;
    using myns::string;
    }

    Now use zone::string everywhere you would have used std::string.

    (And please don't let us think you are redefining 'string' for your own
    nefarious purposes! Leave the std:: classes alone!!)

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


    Comment

    • Fei Liu

      #3
      Re: using std::string; string(&quot;he llo&quot;) vs std::string(&qu ot;hello&quot;) in header file.


      "Phlip" <phlipcpp@yahoo .com> wrote in message
      news:59HWf.2227 7$NS6.20007@new ssvr30.news.pro digy.com...[color=blue]
      > Fei Liu wrote:
      >[color=green]
      > > 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.[/color]
      >
      > Doesn't it say "at top level"?[/color]

      I'll double check that tomorrow when I get a hold of the book again.
      [color=blue]
      >[color=green]
      > > The reason given by the author is that 'using std::string' actually
      > > pollutes
      > > the scope where 'using std::string' is declared.
      > > I find this style is rather inconvenient when I want to experiment with
      > > different implementation of certain classes in large projects. For
      > > example,
      > > I may want to try myns::string or john::string, it's actually much[/color][/color]
      easier[color=blue][color=green]
      > > to
      > > replace 'using std::string' to 'using myns::string' and instantly switch
      > > to
      > > another string implementation. This is much better than going through[/color][/color]
      all[color=blue][color=green]
      > > the instances of 'std::string variable' and replace them to[/color][/color]
      'myns::string[color=blue][color=green]
      > > variable'.
      > >
      > > What are your thoughts on this coding style?[/color]
      >
      > In general, write a friggin "using std::string;" wherever you like, in[/color]
      fresh[color=blue]
      > code where nothing is called "string" except the std:: class.
      >
      > The guideline is to never put anything in a .h file that could cause[/color]
      trouble[color=blue]
      > in any .cpp file that includes it. This rule implies the area outside a
      > class{} or namespace{} block should be reserved for things that absolutely
      > must go there, and convenient things like typedef or using are more[/color]
      trouble[color=blue]
      > than this minor convenience.
      >
      > In your case, you could write this in a header:
      >
      > namespace zone {
      > // using john::string;
      > using myns::string;
      > }
      >
      > Now use zone::string everywhere you would have used std::string.[/color]

      This is a good technique, thanks!
      [color=blue]
      >
      > (And please don't let us think you are redefining 'string' for your own
      > nefarious purposes! Leave the std:: classes alone!!)[/color]

      Think what you will, but I can assure you that redefining 'string' for a C++
      programmer is like taking the first step for a baby. It has to be done
      sooner or later. :)


      Comment

      • Phlip

        #4
        Re: using std::string; string(&quot;he llo&quot;) vs std::string(&qu ot;hello&quot;) in header file.

        > The guideline is to never put anything in a .h file that could cause[color=blue]
        > trouble in any .cpp file that includes it.[/color]

        What kind of trouble? Consider Noah Roberts's recent charming post "Gotta
        love it":
        [color=blue]
        > DComponent(cons t DComponent & copy) : DGenericNode(co py) {}
        >
        > c:\src\PIPE-FLO 32\DComponent.h (12) : error C2065: 'copy' : undeclared
        > identifier
        > c:\src\PIPE-FLO 32\DComponent.h (12) : fatal error C1903: unable to
        > recover from previous error(s); stopping compilation[/color]

        Of course we will never know exactly where that error came from. (And just
        saying "get a less crappy compiler" is less a useful engineering tip than a
        cheap shot at M$.)

        It resembles the kinds of bugs that VC++ has regarding namespaces,
        regardless whether Noah abused his directly. Even if your code is
        syntactically correct, avoiding namespace abuse will reduce the odds of
        these kinds of bugs, whether they are your fault's or the compiler's.

        --
        Phlip
        http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


        Comment

        • Phlip

          #5
          Re: using std::string; string(&quot;he llo&quot;) vs std::string(&qu ot;hello&quot;) in header file.

          Fei Liu wrote:
          [color=blue]
          > Think what you will, but I can assure you that redefining 'string' for a
          > C++
          > programmer is like taking the first step for a baby. It has to be done
          > sooner or later. :)[/color]

          Just so we remain on the same page here, "redefining 'string'" could mean
          any one of these:

          1. write your own string class as an exercise
          2. rebuild std::basic_stri ng<> from scratch
          3. build an alternative app-specific XString class

          Two of those items are attrocities that shouldn't ever be done for any
          reason. Even if you need a string for an application-specific evil character
          set, such as an EBCDIC/UTF-7 hybrid or something, you can still provide a
          custom type for basic_string<>.

          --
          Phlip
          http://www.greencheese.org/ZeekLand <-- NOT a blog!!!



          Comment

          • Fei Liu

            #6
            Re: using std::string; string(&quot;he llo&quot;) vs std::string(&qu ot;hello&quot;) in header file.


            "Phlip" <phlipcpp@yahoo .com> wrote in message
            news:TPIWf.8111 $%m4.1468@newss vr33.news.prodi gy.com...[color=blue]
            > Fei Liu wrote:
            >[color=green]
            > > Think what you will, but I can assure you that redefining 'string' for a
            > > C++
            > > programmer is like taking the first step for a baby. It has to be done
            > > sooner or later. :)[/color]
            >
            > Just so we remain on the same page here, "redefining 'string'" could mean
            > any one of these:
            >
            > 1. write your own string class as an exercise
            > 2. rebuild std::basic_stri ng<> from scratch
            > 3. build an alternative app-specific XString class
            >
            > Two of those items are attrocities that shouldn't ever be done for any
            > reason. Even if you need a string for an application-specific evil[/color]
            character

            There are reasons why a different string implementation would be required.
            It all depends. std::string is a general string implementation that fits
            some (general) cases very well. But to meet specific requirement, be it
            performance, memory management etc, often than not a different domain
            specific string implementation is used.

            Thanks for your feedback.


            Comment

            • Axter

              #7
              Re: using std::string; string(&quot;he llo&quot;) vs std::string(&qu ot;hello&quot;) in header file.

              Fei Liu wrote:[color=blue]
              > 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;
              > class a{
              > string x;
              > };
              >
              > instead, one should write:
              > #include <string>
              > class a{
              > std::string x;
              > };
              >
              > The reason given by the author is that 'using std::string' actually pollutes
              > the scope where 'using std::string' is declared.
              > I find this style is rather inconvenient when I want to experiment with
              > different implementation of certain classes in large projects. For example,
              > I may want to try myns::string or john::string, it's actually much easier to
              > replace 'using std::string' to 'using myns::string' and instantly switch to
              > another string implementation. This is much better than going through all
              > the instances of 'std::string variable' and replace them to 'myns::string
              > variable'.
              >
              > What are your thoughts on this coding style?[/color]

              If you want to experiment, it's much easier if you just create a
              typedef, and then change the typedef when you want to change it
              throughout your code.

              Comment

              • Phlip

                #8
                Re: using std::string; string(&quot;he llo&quot;) vs std::string(&qu ot;hello&quot;) in header file.

                Fei Liu wrote:
                [color=blue]
                > There are reasons why a different string implementation would be required.
                > It all depends. std::string is a general string implementation that fits
                > some (general) cases very well. But to meet specific requirement, be it
                > performance, memory management etc, often than not a different domain
                > specific string implementation is used.[/color]

                I challenge all "build don't buy" decisions.

                But in the specific case of a string, I have worked in linguistics, CORBA,
                ActiveX, object databases, video games, embedded stuff, and chat servers,
                and I have _never_ seen a situation that needs an application-specific
                string class.

                I have, however, seen plenty of custom string classes. They added no value,
                and slowed down development. And their authors often held them for ransom.

                --
                Phlip
                http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


                Comment

                • Joe Van Dyk

                  #9
                  Re: using std::string; string(&quot;he llo&quot;) vs std::string(&qu ot;hello&quot;) inheader file.

                  Phlip wrote:[color=blue]
                  > Fei Liu wrote:
                  >
                  >[color=green]
                  >>There are reasons why a different string implementation would be required.
                  >>It all depends. std::string is a general string implementation that fits
                  >>some (general) cases very well. But to meet specific requirement, be it
                  >>performance , memory management etc, often than not a different domain
                  >>specific string implementation is used.[/color]
                  >
                  >
                  > I challenge all "build don't buy" decisions.
                  >
                  > But in the specific case of a string, I have worked in linguistics, CORBA,
                  > ActiveX, object databases, video games, embedded stuff, and chat servers,
                  > and I have _never_ seen a situation that needs an application-specific
                  > string class.
                  >
                  > I have, however, seen plenty of custom string classes. They added no value,
                  > and slowed down development. And their authors often held them for ransom.[/color]

                  What about a thread-safe string class?

                  Joe

                  Comment

                  • richard@ex-parrot.com

                    #10
                    Re: using std::string; string(&quot;he llo&quot;) vs std::string(&qu ot;hello&quot;) in header file.

                    Joe Van Dyk wrote:
                    [color=blue][color=green]
                    > > I have, however, seen plenty of custom string classes. They added no value,
                    > > and slowed down development. And their authors often held them for ransom.[/color]
                    >
                    > What about a thread-safe string class?[/color]

                    If your std::string class does COW then I would hope it does atomic,
                    lock-free reference-counting which will correctly hide the fact
                    internal data sharing is occuring; if it does not, then it already is
                    thread safe. Of course, that doesn't mean you can't do non-thread-safe
                    things with a string class -- for example, as with any object that is
                    shared between threads, a global std::string object that might get
                    written from multiple threads should have a mutex (or similar) to make
                    it safe. But you wouldn't want to add that overhead to all std::string
                    objects, would you?

                    --
                    Richard Smith

                    Comment

                    Working...