I have a set of homogeneous policy classes that I want to pass as policies for the PolicyDrivenClass template class, which accepts an unknown number of policy template settings.
Each policy implements a name function, and I would like to be able to query the names of all policies at runtime through PolicyDriveClass :: getNames.
I have a working implementation, but it is clumsy, especially considering that in my final design the Policy classes will implement several functions like "name", although it may have different types of returned data, and that my policy-driven class will want to provide access to accessories similar to "getNames" for each of these features.
My question is: can anyone come up with a better implementation for this.
Why use clang ++. My version of g ++ does not like.
Here is what I still have:
#include <string> #include <deque> #include <algorithm> #include <iterator> #include <iostream> using namespace std; template<typename... Policies> class PolicyDrivenClass { public: template<typename T, typename... Types> class NameExtractor { public: static deque<string> getNames() { deque<string> names = NameExtractor<Types...>::getNames(); names.push_front(T::name()); return names; } }; template<typename T> class NameExtractor<T> { public: static deque<string> getNames() { deque<string> ret; ret.push_back(T::name()); return ret; } }; deque<string> getNames() const { return NameExtractor<Policies...>().getNames(); } }; class Policy1 { public: static string name(){return "policy 1";} }; class Policy2 { public: static string name(){return "policy 2";} }; class Policy3 { public: static string name(){return "policy 3";} }; int main() { PolicyDrivenClass<Policy1, Policy2, Policy3> c; deque<string> names = c.getNames(); ostream_iterator<string> out (cerr,"\n"); copy(names.begin(), names.end(), out); }
source share