Connecting Tech Pros Worldwide Forums | Help | Site Map

Correct usage of "using" with standard templates

john.burton.email@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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?


Ioannis Vranos
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Correct usage of "using" with standard templates


john.burton.email@gmail.com wrote:[color=blue]
> 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;[/color]


Yes it is.



[color=blue]
> Or do I need to specify the type too:
> using std::vector<int>;[/color]


No, you do not need to.




--
Ioannis Vranos

http://www23.brinkster.com/noicys
Victor Bazarov
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Correct usage of "using" with standard templates


john.burton.email@gmail.com wrote:[color=blue]
> 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;[/color]

Yes.
[color=blue]
> Or do I need to specify the type too:
> using std::vector<int>;[/color]

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?
[color=blue]
> Both seem to "work" on the compiler I have and I can't find any
> documentation saying which is correct, or are both correct?[/color]

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

V
Ioannis Vranos
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Correct usage of "using" with standard templates


Victor Bazarov wrote:
[color=blue][color=green]
>> Or do I need to specify the type too:
>> using std::vector<int>;[/color]
>
>
> 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?
>[color=green]
>> Both seem to "work" on the compiler I have and I can't find any
>> documentation saying which is correct, or are both correct?[/color]
>
>
> I always use the first one. Hasn't failed me so far...[/color]


#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
red floyd
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Correct usage of "using" with standard templates


john.burton.email@gmail.com wrote:[color=blue]
> 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?
>[/color]

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>

Victor Bazarov
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Correct usage of "using" with standard templates


red floyd wrote:[color=blue]
> john.burton.email@gmail.com wrote:
>[color=green]
>> 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?
>>[/color]
>
> Actually, both are correct.[/color]

Strangely enough as was shown by Ioannis, and as I checked with Comeau,
the second form is incorrect.
[color=blue]
> #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>
>[/color]

But that's not the same as saying

using std::basic_string<char>;

, is it?

V
Buster
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Correct usage of "using" with standard templates


Victor Bazarov wrote:
[color=blue]
> red floyd wrote:
>[color=green]
>> john.burton.email@gmail.com wrote:
>>[color=darkred]
>>> 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?
>>>[/color]
>>
>> Actually, both are correct.[/color]
>
>
> Strangely enough as was shown by Ioannis, and as I checked with Comeau,
> the second form is incorrect.
>[color=green]
>> #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>
>>[/color]
>
> But that's not the same as saying
>
> using std::basic_string<char>;
>
> , is it?[/color]

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.
Micah Cowan
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Correct usage of "using" with standard templates


Buster wrote:
[color=blue]
> Victor Bazarov wrote:
>[color=green]
>> red floyd wrote:
>>[color=darkred]
>>> john.burton.email@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.[/color]
>>
>>
>>
>> Strangely enough as was shown by Ioannis, and as I checked with Comeau,
>> the second form is incorrect.
>>[color=darkred]
>>> #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>
>>>[/color]
>>
>> But that's not the same as saying
>>
>> using std::basic_string<char>;
>>
>> , is it?[/color]
>
>
> 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.[/color]

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.
Ioannis Vranos
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Correct usage of "using" with standard templates


red floyd wrote:
[color=blue]
> 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>[/color]


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
red floyd
Guest
 
Posts: n/a
#10: Jul 23 '05

re: Correct usage of "using" with standard templates


Ioannis Vranos wrote:[color=blue]
> red floyd wrote:
>[color=green]
>> 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>[/color]
>
>
>
> No because in <string> there is a typedef:
>
> typedef basic_string<char> string;
>
>
>
> while in <vector> there is not anything named vector<int>.
>[/color]

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.

Ioannis Vranos
Guest
 
Posts: n/a
#11: Jul 23 '05

re: Correct usage of "using" with standard templates


red floyd wrote:
[color=blue]
> 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.[/color]


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
john.burton.email@gmail.com
Guest
 
Posts: n/a
#12: Jul 23 '05

re: Correct usage of "using" with standard templates



Victor Bazarov wrote:[color=blue]
> john.burton.email@gmail.com wrote:[color=green]
> > I've done some extensive searching and can't seem to find an answer[/color][/color]
to[color=blue][color=green]
> > this -
> >
> > Is it correct to using "using" with templates, for example:
> > using std::vector;[/color]
>
> Yes.
>[color=green]
> > Or do I need to specify the type too:
> > using std::vector<int>;[/color]
>
> 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[/color]
there.[color=blue]
> Does the declaration have the desired effect? Can you use[/color]
vector<int>[color=blue]
> without 'std::' afterwards?
>
> Another interesting question, does it instantiate 'std::vector<int>'[/color]
due[color=blue]
> to that declaration?
>[color=green]
> > Both seem to "work" on the compiler I have and I can't find any
> > documentation saying which is correct, or are both correct?[/color]
>
> I always use the first one. Hasn't failed me so far...[/color]

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)

Victor Bazarov
Guest
 
Posts: n/a
#13: Jul 23 '05

re: Correct usage of "using" with standard templates


john.burton.email@gmail.com wrote:[color=blue]
> Victor Bazarov wrote:
>[color=green]
>>john.burton.email@gmail.com wrote:
>>[color=darkred]
>>>I've done some extensive searching and can't seem to find an answer[/color][/color]
>
> to
>[color=green][color=darkred]
>>>this -
>>>
>>>Is it correct to using "using" with templates, for example:
>>>using std::vector;[/color]
>>
>>Yes.
>>
>>[color=darkred]
>>>Or do I need to specify the type too:
>>>using std::vector<int>;[/color]
>>
>>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[/color]
>
> there.
>[color=green]
>>Does the declaration have the desired effect? Can you use[/color]
>
> vector<int>
>[color=green]
>>without 'std::' afterwards?
>>
>>Another interesting question, does it instantiate 'std::vector<int>'[/color]
>
> due
>[color=green]
>>to that declaration?
>>
>>[color=darkred]
>>>Both seem to "work" on the compiler I have and I can't find any
>>>documentation saying which is correct, or are both correct?[/color]
>>
>>I always use the first one. Hasn't failed me so far...[/color]
>
>
> 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) {[/color]

Naming the iterator 'i' hides the vector. You need to name them
differently to get it to compile.
[color=blue]
> 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)[/color]

OK.
[color=blue]
> 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)[/color]

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
Mike Wahler
Guest
 
Posts: n/a
#14: Jul 23 '05

re: Correct usage of "using" with standard templates



"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:58xDd.34738$NC6.2987@newsread1.mlpsca01.us.to .verio.net...[color=blue]
> john.burton.email@gmail.com wrote:[color=green]
> > Victor Bazarov wrote:[/color][/color]
[color=blue][color=green]
> > 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)[/color]
>
> 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).[/color]

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


john.burton.email@gmail.com
Guest
 
Posts: n/a
#15: Jul 23 '05

re: Correct usage of "using" with standard templates


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.

Closed Thread