C ++ compile-time type checking

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 ); // OK 8-) std::cout << endl; testclass.print( flt_val ); // OK 8-) std::cout << endl; testclass.print( char_val ); // OK 8-) std::cout << endl; testclass.print( short_val); // this compiles OK and works ??? 8-( std::cout << endl; testclass.print( str_val ); // compile error 8-) std::cout << endl; } 

@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.

+4
source share
3 answers

Add private function template

 template<typename T> void print(T); 

which does not need implementation. This should catch all types for which there is no explicit print, and since it is private, it will give an error message.

+4
source

You need to make your print function in the template, and then check if the types match:

 template <typename U> void print(const U & u) { // use std::is_same<typename std::decay<T::Head>::type, typename std::decay<U>::type>::value } 

Here I steal is_same and decay from <type_traits> , but if you do not have C ++ 11, you can either take them from TR1 or Boost, or just write them yourself, as they are very simple classes of type modifiers.

A conditional is best to go to static_assert , which is another C ++ 11 function, but there are similar constructs for C ++ 98/03 that create a compile-time error under a certain condition.

+2
source

You can use your arguments with a non-constant reference, causing them to be the same type. However, you can no longer use it with constant variables or literals.

0
source

Source: https://habr.com/ru/post/1380933/


All Articles