Import Only std::string

Discussion in 'C++' started by cppaddict, Aug 12, 2004.

  1. cppaddict

    cppaddict Guest

    I'd like to avoid typing std::string and std::vector without resorting
    to the "using namespace std" directive, which will import the entire
    standard namespace. Is there a way to import only selected items from
    the std namespace, so to speak?

    Thanks for any ideas,
    cpp
    cppaddict, Aug 12, 2004
    #1
    1. Advertising

  2. cppaddict wrote:
    > I'd like to avoid typing std::string and std::vector without resorting
    > to the "using namespace std" directive, which will import the entire
    > standard namespace. Is there a way to import only selected items from
    > the std namespace, so to speak?


    Make your pick:

    using std::string; // then just type 'string'

    or

    typedef std::string str; // then just type 'str'

    Victor
    Victor Bazarov, Aug 12, 2004
    #2
    1. Advertising

  3. cppaddict

    Andre Kostur Guest

    cppaddict <> wrote in
    news::

    > I'd like to avoid typing std::string and std::vector without resorting
    > to the "using namespace std" directive, which will import the entire
    > standard namespace. Is there a way to import only selected items from
    > the std namespace, so to speak?
    >
    > Thanks for any ideas,
    > cpp
    >


    Uh...

    using std::string;
    using std::vector;

    ?
    Andre Kostur, Aug 12, 2004
    #3
  4. cppaddict

    cppaddict Guest

    >Make your pick:
    >
    > using std::string; // then just type 'string'
    >
    >or
    >
    > typedef std::string str; // then just type 'str'


    Thanks Victor.

    I like the first option. Would you recommend placing it only in my
    header file, or repeating it in the implementation file too, for
    clarity?

    cpp
    cppaddict, Aug 12, 2004
    #4
  5. cppaddict wrote:
    >>Make your pick:
    >>
    >> using std::string; // then just type 'string'
    >>
    >>or
    >>
    >> typedef std::string str; // then just type 'str'

    >
    >
    > Thanks Victor.
    >
    > I like the first option. Would you recommend placing it only in my
    > header file, or repeating it in the implementation file too, for
    > clarity?


    I recommend against any 'using' directives or declarations in the header.
    If you think you need them in the global scope, limit yourself to the
    implementation files only. Of course, it would be best if you could
    place such declarations in the same scope level where they are going to
    be used. Example: if you're working with strings in a single function
    in some module, no sense to put the 'using' declaration at the beginning
    of the module that contains that function. Put it in the function itself.

    Victor
    Victor Bazarov, Aug 12, 2004
    #5
  6. cppaddict

    cppaddict Guest

    >I recommend against any 'using' directives or declarations in the header.
    >If you think you need them in the global scope, limit yourself to the
    >implementation files only.


    Would you object to putting "using std::string;" inside the class:

    class MyClass {
    using std::string;
    private:
    string name_;
    .....
    }

    thanks again,
    cpp
    cppaddict, Aug 12, 2004
    #6
  7. cppaddict wrote:

    >>I recommend against any 'using' directives or declarations in the header.
    >>If you think you need them in the global scope, limit yourself to the
    >>implementation files only.

    >
    >
    > Would you object to putting "using std::string;" inside the class:
    >
    > class MyClass {
    > using std::string;
    > private:
    > string name_;
    > ....
    > }
    >
    > thanks again,
    > cpp


    Nope, but some compilers might object. ;-)

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 13, 2004
    #7
  8. "Victor Bazarov" <> wrote in message
    news:XIOSc.720$09.us.to.verio.net...
    > I recommend against any 'using' directives or declarations in the header.
    > If you think you need them in the global scope, limit yourself to the
    > implementation files only. Of course, it would be best if you could
    > place such declarations in the same scope level where they are going to
    > be used. Example: if you're working with strings in a single function
    > in some module, no sense to put the 'using' declaration at the beginning
    > of the module that contains that function. Put it in the function itself.


    Whoa! You can do that?? Awesome!
    Aguilar, James, Aug 13, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page