android – std::pair behaves different on debug and release builds


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)

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img