Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redeclare template parameter

Tags:

c++

templates

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?

like image 239
Alexey Markov Avatar asked Jan 25 '26 22:01

Alexey Markov


1 Answers

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> { /* ... */ };
like image 80
Quentin Avatar answered Jan 27 '26 12:01

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!