Correct usage of "using" with standard templates

  • Thread starter john.burton.email
  • Start date
J

john.burton.email

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?
 
I

Ioannis Vranos

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.
 
V

Victor Bazarov

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?

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
 
I

Ioannis Vranos

Victor said:
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?




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>
 
R

red floyd

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>
 
V

Victor Bazarov

red said:
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
 
B

Buster

Victor said:
Strangely enough as was shown by Ioannis, and as I checked with Comeau,
the second form is incorrect.


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.
 
M

Micah Cowan

Buster said:
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.
 
I

Ioannis Vranos

red said:
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>.
 
R

red floyd

Ioannis said:
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.
 
I

Ioannis Vranos

red said:
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).
 
J

john.burton.email

Victor said:
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 said:
without 'std::' afterwards?

Another interesting question, does it instantiate 'std::vector<int>' due
to that declaration?


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)
 
V

Victor Bazarov

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
 
M

Mike Wahler

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

john.burton.email

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.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top