skip to Main Content

I have a piece of code that returns an std::pair<> structure as below and it behaves differently on my debug and release builds

  • std::pair<bool, const MyData&>: Works well in debug build, doesn’t work on release build (I get corrupted data)
  • std::pair<bool, const MyData*>: Works well both in debug and release builds
  • std::pair<bool, std::reference_wrapper>: reference_wrapper is created via std::cref, works well both in debug and release builds
    I have a logic as below (it is the version with const MyData&):
std::pair<bool, const MyData&> MyClass::GetData() const
{
   // MyData has some complex data and data structure in it
   // MyData is held in std::map<MyData> myMap

   // Perform some searching operation in the map
   if(isFound)
   {
      return std::make_pair(true, myMap.at(...));
   }
   const static MyData emptyData; 
   return std::make_pair(false, emptyData);
}

Theoretically, I was expecting all cases more or less to work the same. I did some research on the internet and re-read my C++ references, but still could not find a real explanation for such behavior.
I compile my code with XCode 12.0 for iOS and Android projects.

2

Answers


  1. Theoretically, I was expecting all cases more or less to work the same. I did some research on the internet and re-read my C++ references, but still could not find a real explanation for such behavior. I compile my code with XCode X.Y.Z for iOS and Android projects.

    I was expecting all cases more or less to work the same. I did some research on the internet and re-read my C++ references have a piece of code that returns an std::pair<> structure as below and it behaves differently on my debug and release builds

    std::pair<bool, const MyData&>: Works well in debug build, doesn’t work on release build (I get corrupted data)
    std::pair<bool, const MyData*>: Works well both in debug and release builds
    std::pair<bool, std::reference wrapper reference wrapper is created via std::crew, works well both in debug and release builds I have a logic as below (it is the version with const Maita):

    Login or Signup to reply.
  2. std::make_pair will make a copy. It will never return a pair with reference types.

    Your return std::make_pair(true, myMap.at(...)); will return a std::pair<bool, MyData> (copying the result of myMap.at(...)), which converts to a std::pair<bool, const MyData&>, where the returned value’s .second is bound to the temporary that is immediately destroyed. This means your function returns a dangling reference.

    Use std::pair‘s constructor:

    // One of:
    return { true, myMap.at(...) };
    return std::pair<bool, const MyData&>(true, myMap.at(...));
    

    (Also consider using just const MyData*, returning nullptr if it is not found. Barring that, std::optional<std::reference_wrapper<const MyData>> is also a good choice to replace pair with bool)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search