Did you know? - Type aliasing

Did you know?

C++ allows to do this:

include <vector>
using vector = std::vector;

Check it out.

Add a Comment

Comments are closed.

Comments (4)

0 0

Andrea Bigagli said on Apr 1, 2015 09:59 AM:

I think that should either be

1) using vector = std::vector<SOME_TYPE>
or
2) template <typename T> using vector = std::vector<T>

not sure what the OP intended, but I can't see that working as written above
0 0

Sektor said on Apr 2, 2015 08:49 AM:

Andrea: what do you mean "can't see that working"? What's the compiler error message?

Actually the example given above makes no sense because it's shorter to do "using std::vector;".
0 0

Andrea Bigagli said on Apr 2, 2015 09:35 AM:

Sektor,
both gcc (4.9.2) and clang (3.6) complain as follows:

1) GCC:
snippet.cpp:3:16: error: invalid use of template-name 'std::vector' without an argument list
using vector = std::vector;

2) CLANG:
snippet.cpp:3:21: error: use of class template 'std::vector' requires template arguments
using vector = std::vector;


0 0

jbruni said on Apr 2, 2015 01:21 PM:

Actually, you could just do this:


using std::vector;


The name 'vector' will be added to the enclosing namespace.