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? 14 2006 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 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
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 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>
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
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.
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.
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
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.
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
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) 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
"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
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. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
13 posts
views
Thread by Jalal |
last post: by
|
8 posts
views
Thread by Geri Reshef |
last post: by
|
6 posts
views
Thread by AlexD_UK |
last post: by
|
43 posts
views
Thread by markryde |
last post: by
|
30 posts
views
Thread by Pep |
last post: by
|
28 posts
views
Thread by NewToCPP |
last post: by
|
5 posts
views
Thread by dananrg |
last post: by
|
3 posts
views
Thread by Wolfgang Meister |
last post: by
|
7 posts
views
Thread by sami |
last post: by
| | | | | | | | | | |