Go Down

Topic: C++ templates (Read 4 times) previous topic - next topic

aarondc

Something tells me I am going to have to specialise some templates where a memcopy is required to save the last println'd value of a variable. I can modify the comparison portion of the debug routine at the same time. I was considering comparing only a limited part of the value in that instance, although a strcmp makes the code look pretty minimal regardless.

Certainly nicer doing it this way than instantiating each version of the functions add / debug(var) / debug(name + var) by hand.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

Nick Gammon

Using void pointers, and memcpy, are basically side-stepping all of the type-checking that has been built into C over the years.

I'm wondering why you need to do this? I've seen threads before where people try to work around perceived language restrictions without stating what their true goal is. Once revealed, there is usually an easier way to do things.
http://www.gammon.com.au/electronics

aarondc

I am storing references to variables in order to see if they have changed and act accordingly.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

aarondc

This site has been the pretty useful: http://www.cprogramming.com/tutorial/template_specialization.html

Partial template specialization. You can specify types but also values for the template. First time I saw that. That's really cool.

The example he provides

Code: [Select]
template <typename T, unsigned length>
class fixedVector { ... };


is immediately useful for my exercise. :D

That page also answers a question I had been considering, and already decided on the most logical answer.

Quote
A final implementation detail comes up with partial specializations: how does the compiler pick which specialization to use if there are a combination of completely generic types, some partial specializations, and maybe even some full specializations? The general rule of thumb is that the compiler will pick the most specific template specialization--the most specific template specialization is the one whose template arguments would be accepted by the other template declarations, but which would not accept all possible arguments that other templates with the same name would accept.
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

Nick Gammon


I am storing references to variables in order to see if they have changed and act accordingly.


More detail?
http://www.gammon.com.au/electronics

aarondc



I am storing references to variables in order to see if they have changed and act accordingly.


More detail?


Start reading from Debugging library onwards: http://forum.arduino.cc/index.php?topic=169518.msg1261239#msg1261239

Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

aarondc

#21
Jun 21, 2013, 11:06 am Last Edit: Jun 21, 2013, 11:08 am by aarondc Reason: 1
For the people perhaps reading this thread who are actually interested in templates, or at least curious as to what they do, here's an example.

My old header file looked like this:

Code: [Select]

void addDebugVar(String, String&);
void addDebugVar(String, long&);
void addDebugVar(const __FlashStringHelper *, int&, int = false);
void addDebugVar(String, byte&, int = false);
void addDebugVar(String, int&, int = false);

void addDebugPin(const __FlashStringHelper *, int&);
void addDebugPin(String, byte&, int);
void addDebugPin(String, int&);
void addDebugPin(String, byte&);

void debug(String, String);
void debug(String, long);
void debug(String, byte);
void debug(const __FlashStringHelper *, int);
void debug(String, int);
  void debug(String &);
void debug(byte &);
void debug(long &);
void debug(int &);


The basic pattern of the functions is:

void [function name](<descriptive name of a variable>, <variable>); AND
void [function name](<variable>);

The <descriptive name of a variable> could be supplied as a String, or pointer to char, or F() macro, etc.
The <variable> could be any basic type, including - potentially - arrays.

The header file is obviously incomplete, as each function requires versions for the different <descriptive name of a variable> types, at least 2 of (char * and F()) and then a version of the function for each variable type people may be using. And I don't even know all the variable types! Then there are different functions (overloaded, of course) for handling whether the variable being added is a pin or a normal variable. etc, etc, etc.

The same header file, using templates, can be written as follows:

Code: [Select]

template <typename T1, typename T2>
void addDebugVar(T1, T2);

template <typename T1, typename T2>
void addDebugPin(T1, T2);

template <typename T1, typename T2>
void debug(T1, T2);

template <typename T1>
void debug(T1);


The beauty of this new header file, is that every possible combination of <descriptive name of a variable> type and <variable> type is generated for you, on the fly, based on usage of calls to those functions in the code. If you don't call that version of the function, it doesn't get compiled.

Very cool.

If this is old hat to you and you have a good website or ebook describing C++ templates, with examples, I really would appreciate a link!
Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

blckdmp

Bit of a dead thread, but if anyone's still reading it, I'd recommend www.learncpp.com .  Decent section on templates and very well written and approachable style to all the basic C++ concepts.
Cheers,
Jon


aarondc


Bit of a dead thread, but if anyone's still reading it, I'd recommend www.learncpp.com .  Decent section on templates and very well written and approachable style to all the basic C++ concepts.
Cheers,
Jon




Thanks for the heads up, much appreciated.

Windows serial port monitor: Tellurium | Arduino serial port debugging library: DBG | Cusom LCD char generator | Technical questions will only be answered in forum threads

Go Up