I have a template class which takes as its first template argument a foo * pointer. I\'d like to instantiate one of these with a foo located at an absolute address, like so:
Facing the same problem (on an STM32), as a work-around I found function pointer template parameters, like so:
template
class LedToggle
{
public:
void Update()
{
// ...
PORT()->BSRR = mSetReset & mask;
// ...
}
};
constexpr GPIO_TypeDef* Port_C() {
return PORTC;
}
LedToggle led;
Notice that we use a function pointer as template parameter, to a function that returns the desired actual pointer. Inside that function casts are allowed; and since the function is declared constexpr
the compiler may (should) optimize away the actual function call and use the function's return value like a literal.