Import Only std::string

C

cppaddict

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
 
V

Victor Bazarov

cppaddict said:
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
 
A

Andre Kostur

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;

?
 
C

cppaddict

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
 
V

Victor Bazarov

cppaddict said:
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
 
C

cppaddict

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
 
T

Thomas Matthews

cppaddict said:
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
 
A

Aguilar, James

Victor Bazarov said:
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!
 

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
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top