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

Limited namespace

P: n/a
Hi,

I am trying to improve the readability of my code. Currently, there
are many 'std::vector<>' statements of various types. I would like to
reduce this to 'vector<>'. Although I could use 'using namespace
std;', the vector type is the only time I use the standard library and
there are several other namespaces being used (mostly abstracted away
with typedef).

I think it would help the readability of my code if I could generalize
std::vector<> with something like:

'using std::vector as vector;'

Under my very basic test bed this chokes. I'm assuming this is because
vector requires a template. Is there an alternative to using 'using
namespace std;' if using typedef for the many possible template choices
is not preferrable?
Regards,
Rob

Test bed:
GNU C++ Version 3.32 (Apple Computer, Inc. build 1635)
Apple OS 10.3, PowerPC G5

Code example:

#include <vector>

int main()
{
using std::vector as vector; // chokes

vector<int> hi;
hi.push_back(1);

return 0;
}

Errors:

test.cpp:5: error: use of class template `template<class _Tp, class
_Alloc>
class std::vector' as expression
test.cpp:5: error: parse error before `;' token

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ro*************@gmail.com wrote:
I think it would help the readability of my code if I could generalize
std::vector<> with something like:

'using std::vector as vector;'

Under my very basic test bed this chokes.


Indeed. Made-up syntax rarely works. But this does just what you want:

using std::vector;

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

P: n/a
Pete Becker wrote:
ro*************@gmail.com wrote:
I think it would help the readability of my code if I could generalize
std::vector<> with something like:

'using std::vector as vector;'

Under my very basic test bed this chokes.

Indeed. Made-up syntax rarely works. But this does just what you want:

using std::vector;


We could almost make it work with

#define as(a)
...
using std::vector as(vector);

:-)
Jul 23 '05 #3

P: n/a
Victor Bazarov wrote:
We could almost make it work with

#define as(a)
...
using std::vector as(vector);


Why not go the whole way?

#define as ; using std::
using std::vector as vector;
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.