Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

c++ - 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...