I don't know what can I do to make this work in C++.
The intend is:
pair<int, int> foo() {
if(cond) {
return std::make_pair(1,2);
}
return NULL; //error: no viable conversion from 'long' to 'pair<int, int>
}
void boo() {
pair<int, int> p = foo();
if (p == NULL) { //error: comparison between NULL and non-pointer ('int, int' and NULL)
// doA
} else {
int a = p.first;
int b = p.second;
// doB
}
}
Since I cannot use return NULL in C++, here is my 2nd try:
pair<int, int>* foo() {
if(cond) {
return &std::make_pair(1,2); //error: returning address of local temporary object)
}
return nullptr;
}
void boo() {
pair<int, int>* p = foo();
if (p == nullptr) {
// doA
} else {
int a = p->first;
int b = p->second;
// doB
}
}
What is the correct way to be able to return a pair and a null value.
You should use an exception:
std::pair<int, int> foo() {
if(cond) {
return std::make_pair(1,2);
}
throw std::logic_error("insert error here");
}
and in your boo
function:
try {
std::pair<int, int> p = foo();
int a = p.first;
int b = p.second;
} catch (std::logic_error const& e) {
// do something
}
And here's the live example.
In alternative you can use std::optional
(since C++14) or boost::optional
:
std::optional<std::pair<int, int>> foo() {
if(cond) {
return std::make_pair(1,2);
}
return {};
}
std::optional<std::pair<int, int>> p = foo();
if (p) {
int a = p->first;
int b = p->second;
} else {
// do something
}
And here's a the working example with the boost version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With