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

Correct usage of "using" with standard templates

P: n/a
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
Share this Question
Share on Google+
14 Replies


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

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

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

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

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

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

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

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

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

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

P: n/a

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

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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.