Reputation: 417
I tried the example in bjarne stroustrup's c++ programming language 4th edition:
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
template<typename T>
using Iterator<T> = typename T::iterator;
template<typename C, typename V>
vector<Iterator<C>> find_all(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;
}
void test()
{
string m {"Mary had a little lamb"};
for (auto p : find_all(m,'a'))
if (*p!='a')
cerr << "string bug!\n";
// p is a str ing::iterator
list<double> ld {1.1, 2.2, 3.3, 1.1};
for (auto p : find_all(ld,1.1))
if (*p!=1.1)
cerr << "list bug!\n";
vector<string> vs { "red", "blue", "green", "green", "orange", "green" };
for (auto p : find_all(vs,"green"))
if (*p!="green")
cerr << "vector bug!\n";
for (auto p : find_all(vs,"green"))
*p = "ver t";
}
int main()
{
test();
return 0;
}
§4.5.1
I compile the code with: g++ test.cpp -o test -g -std=c++11 on ubuntu, but I got the errors below:
iterator_of_iterator_version_2.cpp:9:15: error: expected ‘=’ before ‘<’ token
using Iterator<T> = typename T::iterator;
^
iterator_of_iterator_version_2.cpp:9:15: error: expected type-specifier before ‘<’ token
iterator_of_iterator_version_2.cpp:12:8: error: ‘Iterator’ was not declared in this scope
vector<Iterator<C>> find_all(C& c, V v)
^
iterator_of_iterator_version_2.cpp:12:17: error: template argument 1 is invalid
vector<Iterator<C>> find_all(C& c, V v)
^
iterator_of_iterator_version_2.cpp:12:17: error: template argument 2 is invalid
iterator_of_iterator_version_2.cpp:12:18: error: expected unqualified-id before ‘>’ token
vector<Iterator<C>> find_all(C& c, V v)
^
iterator_of_iterator_version_2.cpp: In function ‘void test()’:
iterator_of_iterator_version_2.cpp:24:30: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(m,'a'))
^
iterator_of_iterator_version_2.cpp:29:31: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(ld,1.1))
^
iterator_of_iterator_version_2.cpp:33:35: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(vs,"green"))
^
iterator_of_iterator_version_2.cpp:36:35: error: ‘find_all’ was not declared in this scope
for (auto p : find_all(vs,"green"))
^
So what is the problem?
It seems that no syntax error can be found in this code because I just did a copy-paste of the example of the book.
Upvotes: 1
Views: 649
Reputation: 872
Change the beginning from
template<typename T>
using Iterator<T> = typename T::iterator;
to
template<typename T>
using Iterator = typename T::iterator;
Works on my ubuntu 16.04 with probably same compiler setup
Why is it that way? I am not 100percent confident about that, someone else might verify it please.
Writing
using Iterator<T>
is invalid, because it doesnt make sense here. We want Iterator to be a templated typedef that will ask its parameter-type for its generic iterator type.
Iterator<T>
would specialize the template.
E.g. we know it better for a specific type:
template<>
using Iterator<MyClass> = MyClassIterator;
At least that works with normal template classes, i think its the same with using as well.
Upvotes: 6
Reputation: 17016
Change this
template<typename T>
using Iterator<T> = typename T::iterator;
to this
template<typename T>
using Iterator = typename T::iterator;
Upvotes: 0