westfw,
I like your idea of a simple way for users to avoid templates and will include it.
The only way to avoid having the static arrays appear in the code is to use constant indices. You can use the GCC compile time check.
__builtin_constant_p(index)
I didn't do this. I just went back to using a struct and a const array with the addresses.
struct UsartRegister {
volatile uint8_t* ucsra; /**< USART Control and Status Register A */
volatile uint8_t* ucsrb; /**< USART Control and Status Register B */
volatile uint8_t* ucsrc; /**< USART Control and Status Register C */
volatile uint8_t* ubrrl; /**< USART Baud Rate Register Low */
volatile uint8_t* ubrrh; /**< USART Baud Rate Register High */
volatile uint8_t* udr; /**< USART I/O Data Register */
};
static const UsartRegister usart[] = {// initialization follows
If the array appears in the image, they are in RAM so it is not too subtle.
There are lots of pitfalls with templates but they are the heart of the C++ standard libraries. Standard C++ IOStreams are implemented entirely in templates.
Edit: The three books below are hard going. You should master this first:
Programming: Principles and Practice Using C++ by Bjarne Stroustrup Paperback $47.59.
For use of templates and C++ design in general I recommend three books but you need to wait for a new edition of one of these.
First for general C++ design:
Modern C++ Design: Generic Programming and Design Patterns Applied by Andrei Alexandrescu Paperback $41.36. This book was published in 2001 so I expect a newer version. Templates appear in chapter 2.
For advice on how to use C++ features:
Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition) by Scott Meyers Paperback $37.30. This edition was published in 2005.
Finally, the book that that will be published soon:
C++ Templates [Hardcover] David Vandevoorde, Nicolai Josuttis http://www.amazon.com/C-Templates-David-Vandevoorde/dp/0321714121/ref=dp_ob_title_bk.
There is an old edition:
C++ Templates: The Complete Guide by David Vandevoorde, Nicolai Josuttis Hardcover $52.73. It was published in 2002.