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:
The declaration bar<(foo*)0x80103400> myFoo;
is ill-formed because non-type template arguments must be a constant expression, from [temp.arg.nontype]:
A template-argument for a non-type template-parameter shall be a converted constant expression (5.20) of the type of the template-parameter.
And the argument you are passing is not, from [expr.const]:
A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:
— [...]
— areinterpret_cast
(5.2.10);
— [...]
The declaration bar<(foo*)0> huh
works since it does not involve a cast, it's simply a null pointer of type foo*
(0
is special) and so it is a valid constant expression.
You could instead simply pass in the address as a template non-type parameter:
template
struct bar { ... };
bar<0x8013400> myFooWorks;
That is viable.