Reputation: 100
template<class T, template<typename> class Seq>
class SequenceWithApply : public Seq<T*>
{
public:
// 0 arguments, any type of return value:
template<class R>
void apply(R (T::*f)()) {
iterator it = begin();
while(it != end()) {
((*it)->*f)();
it++; }
}
// 1 argument, any type of return value:
template<class R, class A>
void apply(R(T::*f)(A), A a) {
iterator it = begin();
while(it != end()) {
((*it)->*f)(a);
it++; }
}
// 2 arguments, any type of return value:
template<class R, class A1, class A2>
void apply(R(T::*f)(A1, A2),
A1 a1, A2 a2) {
iterator it = begin();
while(it != end()) {
((*it)->*f)(a1, a2);
it++;
}
}
}; ///:~
//: C03:applyGromit2.cpp
// Test applyMember.h
#include "Gromit.h"
#include "applyMember.h"
#include <vector>
#include <iostream>
using namespace std;
int main() {
SequenceWithApply<Gromit, vector> dogs;
for(int i = 0; i < 5; i++)
dogs.push_back(new Gromit(i));
dogs.apply(&Gromit::speak, 1);
dogs.apply(&Gromit::eat, 2.0f);
dogs.apply(&Gromit::sleep, 'z', 3.0);
dogs.apply(&Gromit::sit);
} ///:~
I did not quite understand why compiler complain about iterator
here. Since this snippet code implemente a class SequenceWithApply
based on the template. In this case,
SequenceWithApply
is actually a based class of vector
. iterator should be visible in this base class. I really appreciate that someone can help me figure this out.
Upvotes: 0
Views: 156
Reputation: 61910
The compiler looks for iterator
on first-phase lookup, which is before the template is ever instantiated. In order to know from which type the class derives, the template must be instantiated (so that Seq<T*>
is an actual type). Thus, the compiler never finds iterator
in the base class yet.
You can get around this in two easy ways:
A one-off:
typename Seq<T*>::iterator
A type alias in your derived class:
using iterator = typename Seq<T*>::iterator;
All of these unambiguously specify to which type iterator
belongs, looked up in the second phase of lookup when Seq
and T
are known. More on typename
.
You can do the same for your functions:
A one-off:
Seq<T*>::begin()
this->begin() // if inside a member function
A using declaration:
using Seq<T*>::begin;
Upvotes: 2
Reputation: 69864
The book you're using may be out of date. These days c++ has moved in the direction of using free functions for better decoupling.
Example:
#include <vector>
#include <iostream>
// an example Gromit
struct Gromit
{
Gromit(int index) : index(index) {};
void speak(int i) { std::cout << name() << " speaking " << i << std::endl; }
void eat(float f) { std::cout << name() << " eating " << f << std::endl; }
void sleep(char c, double f) { std::cout << name() << " sleeping " << c << " " << f << std::endl; }
void sit() { std::cout << name() << " sitting" << std::endl; }
private:
std::string name() const {
return "Gromit " + std::to_string(index);
}
int index;
};
// apply some function object to each item in a container
template<class Container, class F>
void apply(Container& container, F f)
{
for (const auto& p : container)
{
f(p);
}
}
int main() {
std::vector<std::unique_ptr<Gromit>> dogs;
for(int i = 0; i < 5; i++)
dogs.emplace_back(new Gromit(i));
using namespace std::placeholders;
// apply dog.speak(1) to each dog in dogs...
apply(dogs, std::bind(&Gromit::speak, _1, 1));
// dog.eat(2.0f) for each dog in dogs...
apply(dogs, std::bind(&Gromit::eat, _1, 2.0f));
// ...etc
apply(dogs, std::bind(&Gromit::sleep, _1, 'z', 3.0));
apply(dogs, std::bind(&Gromit::sit, _1));
}
expected output:
Gromit 0 speaking 1
Gromit 1 speaking 1
Gromit 2 speaking 1
Gromit 3 speaking 1
Gromit 4 speaking 1
Gromit 0 eating 2
Gromit 1 eating 2
Gromit 2 eating 2
Gromit 3 eating 2
Gromit 4 eating 2
Gromit 0 sleeping z 3
Gromit 1 sleeping z 3
Gromit 2 sleeping z 3
Gromit 3 sleeping z 3
Gromit 4 sleeping z 3
Gromit 0 sitting
Gromit 1 sitting
Gromit 2 sitting
Gromit 3 sitting
Gromit 4 sitting
Upvotes: 0