简体   繁体   English

有没有什么东西限制了未来的 C++ 标准引入多个返回值?

[英]Is there something that limits the future C++ standard from introducing multiple return values?

In newer versions of the C++ standard, we are allowed to write functions that have multiple return values such as在较新版本的 C++ 标准中,我们可以编写具有多个返回值的函数,例如

std::tuple<int, std::string, float> someFunction0();

This forces us to call the functions as这迫使我们将函数称为

int a;
std::string last_name;
float f_par;
std::tie(a, last_name, f_par) = someFunction0();

My question is, is there something that prevents the C++ committee from introducing a "simpler" form of multiple return value syntax?我的问题是,有什么东西可以阻止 C++ 委员会引入“更简单”的多返回值语法形式吗? Such as

[int, std::string, float] someFunction1();

which would allow you to declare more inline when calling the function这将允许您在调用 function 时声明更多内联

[int a, std::string last_name, float f_par] = someFunction1();

(There are probably better syntactic solutions than the one that I provided.) (可能有比我提供的更好的语法解决方案。)

Compiler-wise, it shouldn't be an issue, right?编译器方面,这应该不是问题,对吧?

In your example std::tuple<int, std::string, float> someFunction0();在您的示例std::tuple<int, std::string, float> someFunction0(); still returns one tuple object, comprised of multiple sub-objects.仍然返回一个tuple object,由多个子对象组成。

is there something that prevents the C++ committee from introducing a "simpler" form of multiple return value syntax?是否有什么东西阻止 C++ 委员会引入“更简单”的多返回值语法形式?

You can use C++17 structured binding declaration to unpack/destructure it:您可以使用 C++17结构化绑定声明来解包/解构它:

Case 2: binding a tuple-like type案例 2:绑定一个类似元组的类型

float x{}; char y{}; int z{}; std::tuple<float&,char&&,int> tpl(x,std::move(y),z); const auto& [a,b,c] = tpl; // a names a structured binding that refers to x; decltype(a) is float& // b names a structured binding that refers to y; decltype(b) is char&& // c names a structured binding that refers to the 3rd element of tpl; decltype(c) is co`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM