Removing the var ++ template class

Half an hour ago, I discovered the variational parameters of the template, and now I'm fully connected.

I have a static class based abstraction for microcontroller output. I want to group multiple output pins so that I can handle them as one output. The code below works, but I think I would have to finish the recursion on 0 parameters, not 1.

template< typename pin, typename... tail_args > class tee { public: typedef tee< tail_args... > tail; static void set( bool b ){ pin::set( b ); tail::set( b ); } }; template< typename pin > class tee< pin > { public: static void set( bool b ){ pin::set( b ); } }; 

I tried this, but the compiler (gcc) does not seem to take this into account:

 template<> class tee<> : public pin_output { public: static void set( bool b ){} }; 

The error message is long, but essentially says no tee <>. Something is wrong with my tee <> or it is not possible to complete the recursion

+6
source share
1 answer

In your most general case, at least argument 1 ( pin ) is required, so you cannot create a specialization with arguments 0 .

Instead, you should make the most general case, which takes any number of arguments:

 template< typename... > class tee; 

And then create specializations:

 template< typename pin, typename... tail_args > class tee<pin, tail_args...> { public: typedef tee< tail_args... > tail; static void set( bool b ){ pin::set( b ); tail::set( b ); } }; template<> class tee<> { public: static void set( bool b ){} }; 
+6
source

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


All Articles