Created
July 1, 2009 20:00
-
-
Save willb/139011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Trivial example of using virtual dispatch to hide template parameters from clients, as | |
// alluded to here: http://chapeau.freevariable.com/2009/06/a-problem-of-dependent-types.html | |
#include <iostream> | |
#include <list> | |
#include <tr1/memory> | |
class repr { | |
public: | |
virtual ~repr() { } | |
virtual size_t op() = 0; | |
}; | |
template <size_t N> | |
class concrete_repr : public repr { | |
public: | |
virtual ~concrete_repr() { } | |
size_t op() { | |
return N; | |
} | |
}; | |
typedef std::tr1::shared_ptr<repr> p_repr; | |
typedef std::list<p_repr> repr_list; | |
typedef repr_list::iterator repr_it; | |
int main(int c, char *v[]) { | |
repr_list items; | |
items.push_back(p_repr(new concrete_repr<2>())); | |
items.push_back(p_repr(new concrete_repr<3>())); | |
items.push_back(p_repr(new concrete_repr<5>())); | |
items.push_back(p_repr(new concrete_repr<7>())); | |
items.push_back(p_repr(new concrete_repr<11>())); | |
for (repr_it it = items.begin(); it != items.end(); ++it) { | |
std::cout << (*it)->op() << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment