I’m trying to get a regular pointer from an iterator, but a really peculiar thing is happening regarding the types I’m getting. I’ll just post the code, I assume these two snippets are equal to eachother, but tell me if I’m wrong. The code leading up to the snippets is:
CallbackTrigger trigger(triggerParameters);
std::set<CallbackTrigger> triggerSet;
auto result = triggerSet.insert(trigger);
Snippet A:
auto whatIGet = &(*result.first); // whatIGet is type:
// "const std::allocator<CallbackTrigger>::value_type *"
Snippet B:
auto arbitraryStep = *result.first;
auto whatIWanted = &arbitraryStep; // this is type "CallbackTrigger*"
The code that has trouble with this difference and refuses to compile with snippet A, is when I try to push the pointer onto a list
std::list<CallbackTrigger*> listing;
listing.push_back(whatIWanted); // compiles fine
listing.push_back(whatIGet); // error: "cannot convert parameter 1 from
// 'const CallbackTrigger* to 'CallbackTrigger *&&'"
What is going on here?
According to the c++11 standard:
Therefore
const std::allocator<CallbackTrigger>::value_type *is the same asconst CallbackTrigger*. Elements ofset<CallbackTrigger>are of typeconst CallbackTrigger, so that is what you get, when you try getting a pointer directly to elements of the set.For Snippet B
arbitraryStepwill be of typeCallbackTrigger, since*result.firstwould beconst CallbackTriggerand the const is dropped for the type deduction. Therefore&arbitraryStepis of typeCallbackTrigger*(notice the lack ofconst). Also notice, thatwhatIWantedis a pointer to a local variable (arbitraryStep), not the object in the set. Therefore using that might not give you the behaviour you want.Considering this it should be obvious that you need to change the type of your list to
list<const CallbackTrigger*>.