// This example shows why typename is needed. // Don’t confuse typename with typedef. // // • What are the requirements of a set? 🦆 // • What are the requirements of a fussyset? 🦆 // // Try making a fussyset of doubles, instead. #include #include using namespace std; // fussyset is just like a set, except that it doesn’t like ice cream. // This is NOT inheritance--there is no is-a relationship. // Instead, it’s a has-a relationship, with forwarding methods. template class fussyset : set { public: typedef set::iterator iterator; // This won’t compile. Why? void insert(const T &val) { if (val != "ice cream") storage.insert(val); } iterator begin() const { return storage.begin(); } iterator end() const { return storage.end(); } size_t size() const { return storage.size(); } }; int main() { fussyset fs; fs.insert("apple"); fs.insert("cereal"); fs.insert("ice cream"); fs.insert("apple"); cout << "size is " << fs.size() << '\n'; for (fussyset::iterator it=fs.begin(); it!=fs.end(); ++it) cout << *it << ' '; cout << '\n'; for (auto d : fs) cout << d << ' '; cout << '\n'; }