gpt4 book ai didi

c++ - 如何声明在函数范围内定义的结构?

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:00 24 4
gpt4 key购买 nike

假设我有这样一个函数:

auto get_struct() {
struct foo {
int x;
};

return foo{2};
}

我可以这样访问和使用 foo:

decltype(get_struct()) fooDecl;
auto x_val = get_struct().x;

是否可以在不使用 decltype 的情况下定义 fooDecl?如果是,语法会是什么样子?

最佳答案

事实证明,没有 decltype 就无法声明 fooDecl,而且在 C++ 11 之前也不可能。

感谢 Quentin 和 Bo Persson 对此事的评论。

关于c++ - 如何声明在函数范围内定义的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41760126/

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