// 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 funnyset? 🦆 // // Make a funnyset of strings. #include #include using namespace std; // funnyset is just like a set, except that it doesn’t like 42. // This is NOT inheritance--there is no is-a relationship. // Instead, it’s a has-a relationship, with forwarding methods. template class funnyset { public: typedef set::iterator iterator; // This won’t compile. Why? void insert(const T &val) { if (val != 42) storage.insert(val); } iterator begin() const { return storage.begin(); } iterator end() const { return storage.end(); } size_t size() const { return storage.size(); } private: set storage; }; int main() { funnyset ss; ss.insert(3.14159); ss.insert(42.0); ss.insert(2.71828); for (funnyset::iterator it=ss.begin(); it!=ss.end(); ++it) cout << *it << ' '; cout << '\n'; for (auto d : ss) cout << d << ' '; cout << '\n'; }