By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Import Only std::string

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
cppaddict <he***@hello.com> wrote in
news:4p********************************@4ax.com:
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;

?
Jul 22 '05 #3

P: n/a
>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
Jul 22 '05 #4

P: n/a
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
Jul 22 '05 #5

P: n/a
>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
Jul 22 '05 #6

P: n/a
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

Jul 22 '05 #7

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:XI***************@newsread1.dllstx09.us.to.ve rio.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!
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.