Question

I am having trouble getting a using declaration to work that I believe should work:

#include <iostream>
#include <vector>
using namespace std;

template<typename C, typename V>
vector<typename C::iterator> find_all1(C& c, V v)
{
  vector<typename C::iterator> res;
  for (auto p = c.begin(); p!=c.end(); ++p) {
    if (*p == v)
      res.push_back(p);
  }
  return res;
}

template<typename T>
using Iterator<T> = typename T::iterator;

template <typename C, typename V>
vector<Iterator<C>> find_all2(C& c, V v)
{
  vector<Iterator<C>> res;
  for (auto p = c.begin(); p != c.end(); ++p) {
    if (*p == v)
      res.push_back(p);
  }
  return res;
}

int main(int argc, const char *argv[])
{
  return 0;
}

This is basically an example from Stroustrup's book, and it won't compile. The first piece (find_all1) does, but the version of the template that tries to employ the using declaration won't.

The leading portion of the error spew looks like this:

$ make
c++  -g -fPIC -std=c++0x -stdlib=libc++  -c -o iter_tests2.o iter_tests2.cpp
iter_tests2.cpp:18:7: error: no template named 'Iterator'; did you mean 'iterator'?
using Iterator<T> = typename T::iterator;
      ^~~~~~~~
      iterator
/usr/bin/../lib/c++/v1/iterator:415:25: note: 'iterator' declared here
struct _LIBCPP_TYPE_VIS iterator
                        ^
iter_tests2.cpp:18:15: error: partial specialization of alias templates is not permitted
using Iterator<T> = typename T::iterator;
              ^~~

There's more, but I think the important part is about the using declaration.

Was it helpful?

Solution

Your syntax is incorrect, remove the <T>:

template<typename T>
using Iterator = typename T::iterator;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top