473,703 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ 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 2161
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<in t>' 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<in t>' 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<_T y>' : 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_stri ng<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_stri ng<char>


But that's not the same as saying

using std::basic_stri ng<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_stri ng<char>


But that's not the same as saying

using std::basic_stri ng<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_stri ng<char>


But that's not the same as saying

using std::basic_stri ng<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_stri ng<char>

No because in <string> there is a typedef:

typedef basic_string<ch ar> 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_stri ng<char>


No because in <string> there is a typedef:

typedef basic_string<ch ar> 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

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

Similar topics

13
3290
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 and corresponding error massages. I am confused here. Similar codes work fine in Win32 Console Project. Can you please help me to resolve this? Thank you for your helps. Best Regards, Jalal
8
1474
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 given the variable declaration?
6
4518
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 : c:\Research\Code\Visual Studio\TestC++2\TestC++2.h(7): error C2871: 'std' : a namespace with this name does not exist If I try this with other project types, e.g. "Console Application (.NET)" projects, I have no such problem.
43
2723
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 & \ !(v)->vcpu_info->evtchn_upcall_mask) whereas evtchn_upcall_pending is of type unsigned char (and also evtchn_upcall_mask is of type unsigned char).
30
4108
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" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
28
2633
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 executable size. This should not cause any performance issue. So, is it safe to say that if I can offered to have bigger image size I can go ahead and use Templates with out worrying about any other issues?
5
9735
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 dirName" commands via os.popen as below, and then parsing and comparing the text output. Basically, I'd like to compare what the ACLs a directory should have against what the actual ACLs are.
3
2651
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 can I otherwise find out which "using" I have to add for e.g. "StreamWriter" or "File"
7
2458
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 the REST based Facebook API - there is a simple example for its usage as shown below: def simple_web_app(request, api_key, secret_key):
0
9251
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9016
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7864
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6588
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2069
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.