using an absolute pointer address as a template argument

前端 未结 6 2560
攒了一身酷
攒了一身酷 2021-02-19 22:49

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:

6条回答
  •  日久生厌
    2021-02-19 23:47

    First, don't use c-style casts. It doesn't make it explicit what is does.

    Second, you will realize that in order to cast an arbitrary number into a pointer, you need to use reinterpret_cast. This sort of cast is not allowed in constexpr expressions.

    Since template parameters are only possible with constant expressions, an arbitrary number as a pointer is not possible.

    The case where 0 is possible is because 0 is convertible to nullptr, which is constant expression.

提交回复
热议问题