Let's consider the next code:
struct Channel; // somewhere declared
using iter_t = std::set<Channel>::iterator;
std::set<Channel> myset;
std::pair<iter_t, bool> emplaced = myset.emplace(arg1,arg2);
Then emplaced.first
contains iterator to element, emplaced.second
indicates if element was added, or already exist. first and second is not clear as for me. I want to rename this fields:
struct ChannelAddedResult
{
iter_t channelIter;
bool wasAdded;
public: // --- Functions, constructors ---
ChannelAddedResult(std::pair<iter_t, bool> &&pa) {
this->channel = pa.first;
this->added = pa.second;
}
};
This constructor copies values. And here is no profit from rvalue reference. Right?
But how can I convert std::pair<iter_t, bool>
to ChannelAddedResult
? This types are equivalent. So C
-style conversation could look like:
union CarUni {
std::pair<iter_t, bool> pair;
ChannelAddedResult car;
}
// Use
CarUni u;
u.pair = myset.emplace(arg1,arg2);
auto ch = *u.car.channelIter;
bool a = u.car.wasAdded;
This allows to achieve rename without additional copies. May be C
-casting (ChannelAddedResult)emplaced_pair
will do the same work, but it is deprecated in C++. These 2 conversions are dangerous from the point of type safety.
Is there a C++11 way for such conversion?