What is the use of using a class template parameter using an instance variable?

When I came into contact with the programming of the microcontroller (Arduino), I saw the following class for controlling the LED on a specific pin:

template <const uint8_t PIN> class LED { public: LED() { pinMode(PIN, OUTPUT); } void turnOn() { digitalWrite(PIN, HIGH); } void turnOff() { digitalWrite(PIN, LOW); } }; 

I can use it through

 LED<8> led; led.turnOn(); 

so that the LED on pin 8 lights up.

But I ask myself: Why is the contact specified as a template parameter, why not as an instance attribute? What is the use of first class over this?

 class LED { public: LED(uint8_t ledPin) : pin(ledPin) { pinMode(pin, OUTPUT); } void turnOn() { digitalWrite(pin, HIGH); } void turnOff() { digitalWrite(pin, LOW); } private: uint8_t pin; }; 

and use it as follows:

 LED led(8); led.turnOn(); 

Is there an advantage to using the first class over the second or is it just a matter of taste? :)

+7
c ++ coding-style templates arduino
source share
2 answers

"Is there an advantage to using the first class over the second or is it just a matter of taste? :)"

Yes. In the case of a template, the compiler can directly use a numerical value without having to store a variable that saves both memory (the number must be stored in RAM if there is no template) and power (you need an instruction that saves the byte to memory, then load it into the register, and do not load directly into the register).

Both savings are so small in the case of a regular PC that it really does not matter, but in the case of small devices (for example, microcontrollers) with low RAM (~ less than 1 MB) and a slow processor, each byte matters.

However, if you use the template for more contacts (with different numbers), your application will contain several on and off methods (for each contact number used), which increases the size of the executable file. In this case, you save memory for data and processor power at the expense of memory for the application, and then it depends on what you need more - a smaller application with more memory and processor, or vice versa.

+10
source share

As Laethnes notes , this can help in speed and memory consumption. I would also like to point out something more fundamental:

Template parameters force decisions at compile time.

This is a very powerful expression about your code and its use. In your case, I find this appropriate.

Your example uses an Arduino board that has an LED insert. In any reasonable setting for this, you know in advance which digital contact the LED will be on. How you set up your equipment is not something that can change while the program is running.

Creating a template template parameter is strict adherence to this basic assumption; this makes it impossible for the runtime decision to affect the number of LED contacts. If it were a regular constructor, users of your class could perform some calculations at runtime, and then determine which LED is on, depending on the result of these calculations, which, in your case, is probably incorrect.

Of course, perhaps you really need to deal with changing hardware configurations at runtime. If this happens, the template option will only bother you. In general, however, if you can perform forced execution at compile time, this is beneficial to your correctness.

+4
source share

All Articles