Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Import Only std::string (http://www.velocityreviews.com/forums/t284996-import-only-std-string.html)

cppaddict 08-12-2004 05:23 PM

Import Only std::string
 
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

Victor Bazarov 08-12-2004 05:48 PM

Re: Import Only std::string
 
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

Andre Kostur 08-12-2004 05:49 PM

Re: Import Only std::string
 
cppaddict <hello@hello.com> wrote in
news:4p9nh0deubdagp2k6254m5ar5i7bqfa7ko@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;

?

cppaddict 08-12-2004 06:16 PM

Re: Import Only std::string
 
>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

Victor Bazarov 08-12-2004 06:32 PM

Re: Import Only std::string
 
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

cppaddict 08-12-2004 10:13 PM

Re: Import Only std::string
 
>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

Thomas Matthews 08-13-2004 12:50 PM

Re: Import Only std::string
 
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


Aguilar, James 08-13-2004 04:16 PM

Re: Import Only std::string
 

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:XIOSc.720$191.710@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!




All times are GMT. The time now is 08:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.