Search code examples
c++c++11usingstdvector

using vs. typedef for std::vector::iterator


I am having a problem when using the new C++11 using keyword. As far as I understand, it's an alias for typedef. But I cannot get it to compile. I want to define an alias for an iterator of a std::vector. If I use this everything works perfectly.

typedef std::vector<fix_point>::iterator inputIterator;

But if I try:

using std::vector<fix_point>::iterator = inputIterator;

The code doesn't compile with:

Error: 'std::vector<fix_point>' is not a namespace
using std::vector<fix_point>::iterator = inputIterator;
                            ^

Why doesn't this compile?


Solution

  • You just have it backwards:

    using inputIterator = std::vector<fix_point>::iterator;
    

    The alias syntax sort of mirrors the variable declaration syntax: the name you're introducing goes on the left side of the =.