dasfex
dasfex

Reputation: 1260

Using pointer as a template parameter

In C++ I can do something like this:

template <typename T, T* Ptr>
class MyClass {};

I have two questions:

  1. Where can I get a pointer other than nullptr in compile time?

  2. How can I use this language feature? Of course, it would be interesting for practical cases, but it seems that this is a rather specific possibility, so an artificial example will also be suitable.

Upvotes: 0

Views: 91

Answers (1)

Jarod42
Jarod42

Reputation: 217085

You can get a pointer to anything with static storage duration and linkage at compile time:

static const int values[] = {4, 8, 15, 16, 23, 42};
MyClass <const int, values> myClass;

static char magic = '*';
MyClass <char, &magic> myClass2;

Demo

Upvotes: 3

Related Questions