473,472 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Correct usage of "using" with standard templates

I've done some extensive searching and can't seem to find an answer to
this -

Is it correct to using "using" with templates, for example:
using std::vector;
Or do I need to specify the type too:
using std::vector<int>;

Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?

Jul 22 '05 #1
14 2116
jo***************@gmail.com wrote:
I've done some extensive searching and can't seem to find an answer to
this -

Is it correct to using "using" with templates, for example:
using std::vector;

Yes it is.

Or do I need to specify the type too:
using std::vector<int>;

No, you do not need to.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #2
jo***************@gmail.com wrote:
I've done some extensive searching and can't seem to find an answer to
this -

Is it correct to using "using" with templates, for example:
using std::vector;
Yes.
Or do I need to specify the type too:
using std::vector<int>;
I am not sure this should be OK. This form is reserved for bringing
members of that class into the scope, but you specify no members there.
Does the declaration have the desired effect? Can you use vector<int>
without 'std::' afterwards?

Another interesting question, does it instantiate 'std::vector<int>' due
to that declaration?
Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?


I always use the first one. Hasn't failed me so far...

V
Jul 22 '05 #3
Victor Bazarov wrote:
Or do I need to specify the type too:
using std::vector<int>;

I am not sure this should be OK. This form is reserved for bringing
members of that class into the scope, but you specify no members there.
Does the declaration have the desired effect? Can you use vector<int>
without 'std::' afterwards?

Another interesting question, does it instantiate 'std::vector<int>' due
to that declaration?
Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?

I always use the first one. Hasn't failed me so far...

#include <vector>
int main()
{
using std::vector<int>;

vector<int> vec(10);
}

MINGW:
C:\c\temp.cpp In function `int main()':
6 C:\c\temp.cpp syntax error before `<' token

C:\c>cl temp.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.40904 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

temp.cpp
temp.cpp(6) : error C2873: 'std::vector<_Ty>' : symbol cannot be used in
a using
-declaration
with
[
_Ty=int
]
temp.cpp(8) : error C2065: 'vector' : undeclared identifier
temp.cpp(8) : error C2062: type 'int' unexpected

C:\c>


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
jo***************@gmail.com wrote:
I've done some extensive searching and can't seem to find an answer to
this -

Is it correct to using "using" with templates, for example:
using std::vector;
Or do I need to specify the type too:
using std::vector<int>;

Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?


Actually, both are correct.

#include <vector>
#include <string>

using std::vector; // first form
using std::string; // second form -- std::string is typedef for
// an instantiation of std::basic_string<char>

Jul 22 '05 #5
red floyd wrote:
jo***************@gmail.com wrote:
I've done some extensive searching and can't seem to find an answer to
this -

Is it correct to using "using" with templates, for example:
using std::vector;
Or do I need to specify the type too:
using std::vector<int>;

Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?

Actually, both are correct.


Strangely enough as was shown by Ioannis, and as I checked with Comeau,
the second form is incorrect.
#include <vector>
#include <string>

using std::vector; // first form
using std::string; // second form -- std::string is typedef for
// an instantiation of std::basic_string<char>


But that's not the same as saying

using std::basic_string<char>;

, is it?

V
Jul 22 '05 #6
Victor Bazarov wrote:
red floyd wrote:
jo***************@gmail.com wrote:
I've done some extensive searching and can't seem to find an answer to
this -

Is it correct to using "using" with templates, for example:
using std::vector;
Or do I need to specify the type too:
using std::vector<int>;

Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?


Actually, both are correct.

Strangely enough as was shown by Ioannis, and as I checked with Comeau,
the second form is incorrect.
#include <vector>
#include <string>

using std::vector; // first form
using std::string; // second form -- std::string is typedef for
// an instantiation of std::basic_string<char>


But that's not the same as saying

using std::basic_string<char>;

, is it?


No, you're right. It's the name that you're "using", not any particular
entity attached to it ("A /using-declaration/ introduces a name into the
declarative region in which the /using-declaration/ appears.", 7.3.3/1).
So you can't declare that you're "using" just one instantiation of a
template, any more than, say, just one overload of a function.

--
Regards,
Buster.
Jul 22 '05 #7
Buster wrote:
Victor Bazarov wrote:
red floyd wrote:
jo***************@gmail.com wrote:

I've done some extensive searching and can't seem to find an answer to
this -

Is it correct to using "using" with templates, for example:
using std::vector;
Or do I need to specify the type too:
using std::vector<int>;

Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?
Actually, both are correct.


Strangely enough as was shown by Ioannis, and as I checked with Comeau,
the second form is incorrect.
#include <vector>
#include <string>

using std::vector; // first form
using std::string; // second form -- std::string is typedef for
// an instantiation of std::basic_string<char>


But that's not the same as saying

using std::basic_string<char>;

, is it?

No, you're right. It's the name that you're "using", not any particular
entity attached to it ("A /using-declaration/ introduces a name into the
declarative region in which the /using-declaration/ appears.", 7.3.3/1).
So you can't declare that you're "using" just one instantiation of a
template, any more than, say, just one overload of a function.


The specific reference that forbids std::vector<int> is 7.3.3/5,
which simply states that "a using declaration shall not name a
template-id." Since a template-id is a purely syntactic entity,
it says nothing to forbid name forms.
Jul 22 '05 #8
red floyd wrote:
Actually, both are correct.

#include <vector>
#include <string>

using std::vector; // first form
using std::string; // second form -- std::string is typedef for
// an instantiation of std::basic_string<char>

No because in <string> there is a typedef:

typedef basic_string<char> string;

while in <vector> there is not anything named vector<int>.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9
Ioannis Vranos wrote:
red floyd wrote:
Actually, both are correct.

#include <vector>
#include <string>

using std::vector; // first form
using std::string; // second form -- std::string is typedef for
// an instantiation of std::basic_string<char>


No because in <string> there is a typedef:

typedef basic_string<char> string;

while in <vector> there is not anything named vector<int>.


OK, I stand corrected (my copy of the Holy Standard arrived yesterday, I
haven't had a chance to read it yet). But it seems odd that I have a
using clause for a typedef which is a template instantiation, but not
the instantiation itself.

Jul 22 '05 #10
red floyd wrote:
OK, I stand corrected (my copy of the Holy Standard arrived yesterday, I
haven't had a chance to read it yet). But it seems odd that I have a
using clause for a typedef which is a template instantiation, but not
the instantiation itself.

Well, to make it easier to understand, using statements bring in scope
*names* already defined in a namespace, and there is no vector<int>
*name* defined in <vector> (and such a name (templatised) can't be
defined anyway).


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #11

Victor Bazarov wrote:
jo***************@gmail.com wrote:
I've done some extensive searching and can't seem to find an answer to this -

Is it correct to using "using" with templates, for example:
using std::vector;
Yes.
Or do I need to specify the type too:
using std::vector<int>;


I am not sure this should be OK. This form is reserved for bringing
members of that class into the scope, but you specify no members

there. Does the declaration have the desired effect? Can you use vector<int> without 'std::' afterwards?

Another interesting question, does it instantiate 'std::vector<int>' due to that declaration?
Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?


I always use the first one. Hasn't failed me so far...


Yes I'm sorry I incorrecly said the 2nd form compiled. I made a mistake
and wasn't compiling the program I thought I was.

A related question though. In the following program -

#include <iostream>
#include <vector>

using std::vector;
using std::cout;

int main()
{
vector<int> i;
for(vector<int>::iterator i = i.begin(); i != i.end(); ++i) {
cout << *i << "\n";
}
}

(please ignore any stupid errors I'm just typing this into this message
and it's not possible for me to try it)

It doesn't compile because it can't find
vector<int>::iterator

I would have assumed that the "using std::vector" would have brough the
iterator class into scope too as a member of vector but it doesn't seem
to have.
Is this correct? Is there another way to do this? Or is it just a
deficiency of the compiler? (I can only try this on VC++6 at present)

Jul 22 '05 #12
jo***************@gmail.com wrote:
Victor Bazarov wrote:
jo***************@gmail.com wrote:
I've done some extensive searching and can't seem to find an answer
to
this -

Is it correct to using "using" with templates, for example:
using std::vector;
Yes.

Or do I need to specify the type too:
using std::vector<int>;


I am not sure this should be OK. This form is reserved for bringing
members of that class into the scope, but you specify no members


there.
Does the declaration have the desired effect? Can you use


vector<int>
without 'std::' afterwards?

Another interesting question, does it instantiate 'std::vector<int>'


due
to that declaration?

Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?


I always use the first one. Hasn't failed me so far...

Yes I'm sorry I incorrecly said the 2nd form compiled. I made a mistake
and wasn't compiling the program I thought I was.

A related question though. In the following program -

#include <iostream>
#include <vector>

using std::vector;
using std::cout;

int main()
{
vector<int> i;
for(vector<int>::iterator i = i.begin(); i != i.end(); ++i) {


Naming the iterator 'i' hides the vector. You need to name them
differently to get it to compile.
cout << *i << "\n";
}
}

(please ignore any stupid errors I'm just typing this into this message
and it's not possible for me to try it)
OK.
It doesn't compile because it can't find
vector<int>::iterator

I would have assumed that the "using std::vector" would have brough the
iterator class into scope too as a member of vector but it doesn't seem
to have.
Is this correct? Is there another way to do this? Or is it just a
deficiency of the compiler? (I can only try this on VC++6 at present)


VC++ v6 has a bug that prevents it from finding vector<int>::iterator.
It is fixed in VC++ 7.1 (at least that's what I can check with).

Victor
Jul 22 '05 #13

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:58******************@newsread1.mlpsca01.us.to .verio.net...
jo***************@gmail.com wrote:
Victor Bazarov wrote: I would have assumed that the "using std::vector" would have brough the
iterator class into scope too as a member of vector but it doesn't seem
to have.
Is this correct? Is there another way to do this? Or is it just a
deficiency of the compiler? (I can only try this on VC++6 at present)


VC++ v6 has a bug that prevents it from finding vector<int>::iterator.
It is fixed in VC++ 7.1 (at least that's what I can check with).


John:

I've run into this bug, too. The workaround is explicit
qualification:

std::vector<int>::iterator

(or you can create and use a typedef for this).

-Mike
Jul 22 '05 #14
Ah excellent. It should work then.
I'll be using a recent version of VC for the project I want to use this
in so that's fine.

Thanks for everyones help. I feel I understand now.

Jul 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Jalal | last post by:
I am trying to use numeric_limits<double>::min() in an MFC application in Microsoft Visual C++.NET 2003. I am having some difficulties here. The following are the parts of a simple program I wrote...
8
by: Geri Reshef | last post by:
Many times I find code examples in the internet which don't have the "using" statements needed to run them, and without it the compilation fails. Is there a way to find the correct "using" statement...
6
by: AlexD_UK | last post by:
When I create a new C++ project of type "Class Library (.NET)", I am unable to then add the following line of code : using namespace std If I do, I get the following error on compilation :...
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
5
by: dananrg | last post by:
Is there a standard library module in Python 2.4 (Win32) that will return directory permissions / ACLs (e.g. users, groups, and what rights they have)? Otherwise, I'm faced with sending "cacls...
3
by: Wolfgang Meister | last post by:
Is there a way to let VisualStudio insert automatically (after pressing e.g. F7) missing "Using" statements at the top ? Or is there a menu which suggests inserts for using directives ? How...
7
by: sami | last post by:
Hi I am trying to write a facebook application in python - I have been programming simple desktop applications till now and am not really familiar with web apps Pyfacebook is the wrapper for...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.