如何在编译时查询constexpr std :: tuple? [英] How to query a constexpr std::tuple at compile time?
问题描述
在C ++ 0x中,可以创建一个constexpr std :: tuple,例如像
In C++0x, one can create a constexpr std::tuple, e.g. like
#include <tuple>
constexpr int i = 10;
constexpr float f = 2.4f;
constexpr double d = -10.4;
constexpr std::tuple<int, float, double> tup(i, f, d);
一个人也可以在运行时查询std :: tuple,例如通过
One also can query a std::tuple at runtime, e.g. via
int i2 = std::get<0>(tup);
但是在编译时无法查询它,例如
But it is not possible to query it at compile time, e.g.,
constexpr int i2 = std::get<0>(tup);
会引发编译错误(至少使用最新的g ++
快照2011-02- 19)。
will throw a compilation error (at least with the latest g++ snapshot 2011-02-19).
还有其他方法可在编译时查询constexpr std :: tuple吗?
如果不是,是否有概念上的原因为什么不应该查询它?
And if not, is there a conceptual reason why one is not supposed to query it?
(我知道避免使用std :: tuple,例如,通过使用boost :: mpl
或boost :: fusion来代替,但是听起来有点不对。错误的是不在新标准中使用元组类
...)。
(I am aware of avoiding using std::tuple, e.g., by using boost::mpl or boost::fusion instead, but somehow it sounds wrong not to use the tuple class in the new standard...).
顺便说一句,有人知道为什么吗
By the way, does anybody know why
constexpr std::tuple<int, float, double> tup(i, f, d);
可以编译,但是
constexpr std::tuple<int, float, double> tup(10, 2.4f, -10.4);
不是吗?
多谢!
-拉尔斯
Thanks a lot in advance! - lars
推荐答案
std :: get
不是标记为 constexpr
,因此您不能使用它从 constexpr中的
上下文,即使该元组本身就是 tuple
中检索值。 constexpr
。
std::get
is not marked constexpr
, so you cannot use it to retrieve the values from a tuple
in a constexpr
context, even if that tuple is itself constexpr
.
不幸的是, std :: tuple
是不透明的,因此您也不能编写自己的访问器。
Unfortunately, the implementation of std::tuple
is opaque, so you cannot write your own accessors either.
这篇关于如何在编译时查询constexpr std :: tuple?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!