oPossum

Volt/Amp/Watt meter

27 posts in this topic

Using templates allows the compiler to do compile time optimization that is not possible with C or C++ classes. It knows the pins and ports won't change at run time, so it can generate optimal code for I/O. This results in code that is smaller, faster and uses less RAM.

 

The compiler will in-line optimise the methods if required (helps if declared "inline") - if the ports definitions are const (which they are) the optimisations should be exactly the same as a templated class - you instantiate a static instance of the LCD class anyway... not sure what, if anything, is gained from using templates here other than a whole bunch of typing... just wondering...

 

Further, using an initialiser list in the LCD constructor would allow class-local constants to be fixed during construction which is somewhat neater than using blank namespace or static consts and creates better encapsulation (compile -O3 or -finline-functions).

 

There should be no extra costs in memory/stack/efficiency in using this method, but there'd be a bunch less text and the code would arguably be a load clearer   :smile:

 

Nice project, BTW.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now