Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pairs and pointers

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.

like image 968
vtlinh Avatar asked Jan 10 '14 02:01

vtlinh


1 Answers

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.

like image 58
Shoe Avatar answered Oct 27 '22 01:10

Shoe