C ++是否具有标准的编译时类型容器? [英] Does C++ have a standard, compile-time type container?

查看:44
本文介绍了C ++是否具有标准的编译时类型容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

((此问题的答案是

(This question has an answer by Nim which mentions boost::mpl::map.)

标准C ++中是否有一个可以容纳类型的编译时容器?

Is there a compile-time container in standard C++ which can hold types?

一个用法示例为:

compiler::vector foo{char, short, long, long long};

template <int N>
void bar(foo[N] param){/*do something and return foo[N]*/}

推荐答案

此版本位于Bérenger"的答案,这使我进入了 tuple 概念.但是我相信我们可以做得更好,甚至保留参考文献:

This build's on Bérenger's answer, which put me onto the tuple concept. But I believe we can do better, even preserving references:

tuple foo<char&, short&, long&, long long&>;

template <int N>
void bar(tuple_element_t<N, decltype(foo)> param){}

实际上,如果没有计划在此功能之外使用 foo ,我们甚至可以内联声明它:

In fact if there are no plans to use foo beyond this function we can even declare it inline:

template <int N>
void bar(tuple_element_t<N, tuple<char&, short&, long&, long long&>> param){}

这篇关于C ++是否具有标准的编译时类型容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆