I am working on C++ template HAL (Hardware Abstraction Library). I trying to create register abstraction which takes register's address as a template parameter as uint32_t or as pointer, but I don't know how to do it because it causes template parameter overloading.
Incorrect example which cause redeclare parameter error:
template<uint32_t addr>
struct reg
{
...
};
template<uint32_t* addr>
struct reg
{
...
};
I thought about template specialization:
template<class T>
struct reg {};
template<>
struct reg<uint32_t>
{
...
};
template<>
struct reg<uint32_t*>
{
...
};
But I can't get the actual address value this way.
Is there any way to do this?
You're close, an auto template parameter solves it neatly:
template <auto addr>
struct reg;
template <std::uintptr_t addr>
struct reg<addr> { /* ... */ };
template <auto *addr>
struct reg<addr> { /* ... */ };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With