如何在编译时查询 constexpr std::tuple?
在 C++0x 中,可以创建一个 constexpr std::tuple,例如,
#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,例如 via
int i2 = std::get<0>(tup);
但不可能在编译时查询它,例如,
constexpr int i2 = std::get<0>(tup);
会抛出编译错误错误(至少对于最新的 g++ 快照 2011-02-19)。
是否有其他方法可以在编译时查询 constexpr std::tuple?
如果没有,是否存在不应该查询它的概念原因?
(我知道避免使用 std::tuple,例如,通过使用 boost::mpl 或 boost::fusion 代替,但不知怎的,不使用 tuple 类听起来是错误的 在新标准中...)。
顺便问一下,有人知道为什么
constexpr std::tuple<int, float, double> tup(i, f, d);
编译得很好,但编译
constexpr std::tuple<int, float, double> tup(10, 2.4f, -10.4);
不行吗?
预先非常感谢! - 拉斯
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);
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);
will throw a compilation error (at least with the latest g++
snapshot 2011-02-19).
Is there any other way to query a constexpr std::tuple at compile time?
And if not, is there a conceptual reason why one is not supposed to query it?
(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);
compiles fine, but
constexpr std::tuple<int, float, double> tup(10, 2.4f, -10.4);
not?
Thanks a lot in advance!
- lars
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
std::get
未标记constexpr
,因此您无法使用它从constexpr
中的tuple
检索值code> 上下文,即使该元组本身就是constexpr
。不幸的是,std::tuple 的实现是不透明的,因此您也无法编写自己的访问器。
std::get
is not markedconstexpr
, so you cannot use it to retrieve the values from atuple
in aconstexpr
context, even if that tuple is itselfconstexpr
.Unfortunately, the implementation of
std::tuple
is opaque, so you cannot write your own accessors either.现在, std::get<>是一个 constexpr 函数。如果我使用 gcc c++ 11 或更高版本,则可以编译以下代码。
此外,您可以使用 make_index_sequence (c++14 或更高版本)在编译时生成数字列表并访问元组。
Now, std::get<> is a constexpr function. The following code compiles for me if I use gcc c++ 11 or above.
Furthermore, you can generate a list of numbers at compile time by using make_index_sequence (c++14 or above) and access the tuple.
此问题已在 c++17 中修复。但是如果您想将元组作为参数传递到 constexpr 函数中,我强烈建议您阅读这篇文章 https://mpark.github.io/programming/2017/05/26/constexpr-function-parameters/
This issue was fixed in c++17. But if you want to pass tuple into the constexpr function as a parameter, i would strongly recommend to read this post https://mpark.github.io/programming/2017/05/26/constexpr-function-parameters/
我还没有使用过 C++0x,但在我看来 std::get() 是一个函数,而不是编译器可以直接解释的表达式。因此,除了在函数本身被编译之后的运行时之外,它没有任何意义。
I have not yet worked with C++0x, but it seems to me that std::get() is a function, rather than expression the compiler can directly interpret. As such, it has no meaning except at runtime, after the function itself has been compiled.