gpt4 book ai didi

c++ - 如果没有 'auto' ,是否有我无法使用的功能(特别是结构化绑定(bind))?

转载 作者:太空狗 更新时间:2023-10-29 21:12:33 26 4
gpt4 key购买 nike

auto引入关键字以简化代码。特别是,无需使用丑陋的 std::vector<MyType>::iterator,迭代 STL 容器变得更容易和更好看。每次你想遍历它时的语法。但是,仍然可以在不使用 auto 的情况下编写代码。这将做完全相同的事情。

现在(我认为)如果没有 auto 就无法使用某些功能,特别是结构化绑定(bind):

std::tuple<int, int&> f();
auto [x, y] = f();

那么,两个问题:

  1. 没有办法初始化[x, y],我说得对吗?不使用auto (仍在使用结构化绑定(bind))?有没有办法明确地初始化它:*explicit_type* [x, y] = f();
  2. 还有哪些功能需要使用 auto

最佳答案

Am I correct that there's no way to initialize [x, y] without using auto (still using structured bindings)?

是的,这是正确的。语法没有指定其他方式,例如可以看出 here .

What other features require using auto?

一个经典的例子应该是(通用的)lambda 表达式:

auto lambda = [](auto&&...) { };

但正如评论中所述,还有一些其他示例。

关于c++ - 如果没有 'auto' ,是否有我无法使用的功能(特别是结构化绑定(bind))?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47125892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com