簡體   English   中英

有沒有什么東西限制了未來的 C++ 標准引入多個返回值?

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

在較新版本的 C++ 標准中,我們可以編寫具有多個返回值的函數,例如

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

這迫使我們將函數稱為

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

我的問題是,有什么東西可以阻止 C++ 委員會引入“更簡單”的多返回值語法形式嗎?

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

這將允許您在調用 function 時聲明更多內聯

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

(可能有比我提供的更好的語法解決方案。)

編譯器方面,這應該不是問題,對吧?

在您的示例std::tuple<int, std::string, float> someFunction0(); 仍然返回一個tuple object,由多個子對象組成。

是否有什么東西阻止 C++ 委員會引入“更簡單”的多返回值語法形式?

您可以使用 C++17結構化綁定聲明來解包/解構它:

案例 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