Return 在调用 returns std::pair 的函数时仅 std::pair 的第一个元素
Return only the first element of an std::pair when calling a function that returns std::pair
我正在使用 return 一个 std::pair
:
的函数
std::pair<bool, int> myFunction() {
//Do something...
if (success) {
return {true, someValue};
}
else {
return {false, someOtherValue};
}
}
成功时,该对的第一个值将为 true
,否则为 false
。
一些调用 myFunction()
的函数使用 returned 对的第二个值,其他函数则不使用。对于那些,我这样称呼 myFunction()
:
bool myOtherFunction() {
//Do something...
bool success;
std::tie(success, std::ignore) = myFunction(); //I don't care about the pair's second value
return success;
}
有没有办法避免直接声明 bool success
和 returning myFunction()
的 return 值的第一个元素?
a std::pair
只是一个有 2 个值的结构;所以只有 return 结构中的 "first" 项。
return myFunction().first;
也许
return std::get<0>(myFunction());
或
return std::get<bool>(myFunction());
我正在使用 return 一个 std::pair
:
std::pair<bool, int> myFunction() {
//Do something...
if (success) {
return {true, someValue};
}
else {
return {false, someOtherValue};
}
}
成功时,该对的第一个值将为 true
,否则为 false
。
一些调用 myFunction()
的函数使用 returned 对的第二个值,其他函数则不使用。对于那些,我这样称呼 myFunction()
:
bool myOtherFunction() {
//Do something...
bool success;
std::tie(success, std::ignore) = myFunction(); //I don't care about the pair's second value
return success;
}
有没有办法避免直接声明 bool success
和 returning myFunction()
的 return 值的第一个元素?
a std::pair
只是一个有 2 个值的结构;所以只有 return 结构中的 "first" 项。
return myFunction().first;
也许
return std::get<0>(myFunction());
或
return std::get<bool>(myFunction());