c++ - c++ 中没有 std::tuple 的 std::iterator 有什么原因吗?
问题描述
std::iterator
有一个for似乎很自然std::tuple
。但是它没有实现,所以程序员实现了自己的版本。例如,可以在Jonathan Müller 的博客中找到一个。
我忽略了什么吗?tuple_iterator 没有“官方版本”有什么原因吗?
解决方案
std::tuple
不是容器,至少在标准库容器的意义上不是。它不能用通常的方法迭代,因为它包含不同类型的对象。标准容器是同构容器,而标准容器是std::tuple
异构容器。
这并不意味着你不能迭代一个元组,但它非常麻烦并且你不能使用已建立的语法:
让我们假设您想以与迭代容器相同的方式迭代元组,例如std::vector
:
std::tuple<int, std::string, bool> t = {...};
for (auto it = t.begin(); it != t.end(); ++it)
{
auto elem = *it; // this cannot work the same way as with containers
// because each element is of a different type
}
你可以做几件事。使用std::variant
包含容器类型的迭代器。访问下面的真实对象并不容易。而且这种迭代器也不能在标准库中需要迭代器的其他任何地方使用,至少在没有一些额外工作的情况下不能使用。
还有一项关于元编程(反射,内省)的提案的工作,该提案具有(或曾经,未遵循)以下语法:
std::tuple<int, std::string, bool> t = {...};
// something like this (can't remember the syntax exactly):
for constexpr ... (auto elem : t)
{
// only the common set of operations permitted with `elem`
// e.g. this is valid
std::cout << elem << std::endl;
// this is invalid:
//elem.size();
}
这实际上会在编译时展开,从而产生以下代码:
std::tuple<int, std::string, bool> t = {...};
{
int elem = std::get<0>(t);
std::cout << elem << std::endl;
}
{
std::string elem = std::get<1>(t);
std::cout << elem << std::endl;
}
{
bool elem = std::get<2>(t);
std::cout << elem << std::endl;
}
推荐阅读
- android - “center_horizontal与属性重力不兼容......” xml新手,我不明白我的问题是什么
- windows - Windows 直接关闭 Tkinter 主循环
- schema - JSONschema 中键 A 的值等于键 B 的值
- javascript - 在切换方法之后测试子组件是否有条件地呈现
- azure-devops - 使用 Azure 的 Terraform - 资源操作符号未在日志中正确显示
- sql-server - 将 SAS 日期时间转换为 SQL 格式
- python - 您如何有效地缓存大型 django 查询集?
- sql-server - SQL 或 SAS 函数,用于根据其他类似记录估算分类变量
- javascript - 限制并发上传
- ssl - 如何解决 Heroku 上的 Cloudflare DNS 错误 526?