You can't as-is, because non-type template arguments of pointer type can only be null pointer expressions or addresses of objects, and a conversion from an integer is neither of those.
You could redesign the template slightly:
template <uintptr_t portAddr>
class PortWrapper {
private:
static constexpr Port* port() { return (Port*)portAddr; }
public:
PortWrapper() {
port()->reg1 = 1;
port()->reg2 = 2;
}
};
Note that in the comments, @KonradRudolph disputes whether this strictly follows the rules for a constexpr
function, and it's quite possible it does not. Nevertheless, even if constexpr
is omitted from the above, any decent compiler will inline the call of port()
, effectively resulting in compile-time evaluation.