C++ pairs and pointers

13,718

Solution 1

Try passing in a pair to foo() by reference, then returning a bool from that function indicating success or failure. Like so:

bool foo(pair<int, int>& myPair) {
  if(cond) {
    myPair = std::make_pair(1,2);
    return true;
  }
  return false;
}
void boo() {
  pair<int, int> myPair;
  if (!foo(myPair)) {
    // doA
  } else {
    int a = myPair.first;
    int b = myPair.second;
    // doB
  }
}

Edit: Depending on what you're doing, you should just kill foo() if possible and evaluate cond within boo()

void boo() {
  pair<int, int> myPair;
  if (!cond) {
    // doA
  } else {
    myPair = std::make_pair(1,2);
    int a = myPair.first;
    int b = myPair.second;
    // doB
  }
}

Solution 2

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.

Share:
13,718
vtlinh
Author by

vtlinh

Updated on June 18, 2022

Comments

  • vtlinh
    vtlinh about 2 years

    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.