Limited namespace

R

robert.ferguson

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
 
P

Pete Becker

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

Victor Bazarov

Pete said:
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);

:)
 
D

Dietmar Kuehl

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

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,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top