如何构建这个 c++将列表键入变体?
Here,
how do I fix this c++ typelist template compile error?
we built a typelist, using the code from modern c++ design.
Question is now -- how do I take this and built it into a variant class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用案例:
Use case :
正确但更高级的方法实际上是将值存储在知道如何管理其包含的实际类型的持有者类型中。
出于学习目的,一种更简单的方法是将类型映射到数字(即它们在类型列表中的位置)。这样您就可以记住当前在变体中存储的类型。
为了获得一个工作版本,您可能需要模板化的构造函数和设置器,以及使用该类型-数字映射的访问器函数。
非常简化,它可能看起来像这样:
The proper, but more advanced, approach would be actually store the values in a holder type that knows how to manage the actual type it contains.
A simpler approach, for learning purposes, would be to map types to numbers (i.e. their position in the typelist). With that you can remember what type you are currently storing in the variant.
To get that a working version you'll probably want to have templated constructors and setters and also an accessor function that use that type-number mapping.
Quite simplified it could look something like this: