I created a list of types. Then I create a class using a template that passes a list of types. When I call the print function of a class with some types not specified, they are issued. How can I apply the exact type at compile time? Therefore, if I use an unregistered type, I get a compiler error. Thank you
template <class T, class U> struct Typelist { typedef T Head; typedef U Tail; }; class NullType { }; typedef Typelist<int,Typelist<float,Typelist<char*,NullType> > > UsableTypes; template<class T> class MyClass { public: void print(T::Head _Value) { std::cout << _Value; } void print(T::Tail::Head _Value) { std::cout << _Value; } void print(T::Tail::Tail::Head _Value) { std::cout << _Value; } private: }; MyClass<UsableTypes> testclass; void TestMyClass() { int int_val = 100000; float flt_val = 0.1f; char* char_val = "Hi"; short short_val = 10; std::string str_val = "Hello"; testclass.print( int_val );
@Kerrek SB: Hi, I thought this would help me with my next step, which created a print function depending on the contents of t_list, types and the number of types. But I'm struggling to separate compilation time processing and runtime processing. What I'm trying to do is create a print function for each type in the list. Therefore, if the list has two types, two print functions will be created, and if there are five types, then five print functions will be created, one for each type. When I do this:
typedef Typelist<int,Typelist<float,Typelist<char*,NullType> > > UsableTypes; MyClass<UsableTypes> newclass
Does this create three instances of MyClass for each type in the list, or does it create one instance, and do I need to create a print function for each type? I feel that I have almost all the blocks in my head, but I just canβt combine them. Any help you can offer would be greatly appreciated. Thanks.
source share