Does this scheme determine the inheritance time for compiling a name?

I recently developed the C ++ 11 library, and I came to use this template a couple of times when the class that will actually be exposed to the user defines its inherited classes based on. I reproduced here the variational metaphor va_if that I use for this, but this could be done using boost::mpl::if_c or std::conditional .

 template<typename bool_type, typename result_type, typename... Ts> struct va_if; template<typename result_type, typename... Ts> struct va_if<std::true_type, result_type, Ts...> { typedef result_type type; }; template<typename result_type, typename... Ts> struct va_if<std::false_type, result_type, Ts...> { typedef typename va_if<Ts...>::type type; }; template<typename T> class container_base { /* Generic container functions */ }; template<typename T> class container_integral_base : public container_base<T> { /* Code tailored to integral types */ }; template<typename T> class container_floating_point_base : public container_base<T> { /* Code tailored to floating point types */ }; // This class chooses the class it should inherit from at compile time... template<typename T> class Container : public va_if<std::is_integral<T>::type, container_integral_base<T>, std::is_floating_point<T>::type, container_floating_point_base<T>>::type { /* public interface code */ }; 

I am wondering if this inheritance definition template has a compile time name?

+4
source share
1 answer

I don’t think I saw this template used with variation templates before, although a similar approach used with specialization can be seen in several libraries:

 enum container_type { generic, arithmetic, floating_point }; template <container_type, typename T> struct container_base { // generic //... }; template <typename T> struct container_base<arithmetic,T> { // replaces container_integral_base //... }; template <typename T> struct container_base<floating_point,T> { // replaces container_floating_point_base //... }; 

There is still no name for it, but I would consider replacing your unnamed template with this other unnamed more general template, which you can briefly describe as inheritance by specialization

+2
source

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


All Articles