The key of the set
is declared as being a pointer-to-int, an int*
. But this:
void push( const B& b )
{
set.insert( &b.x );
}
is passing the address of a constant int
, an int const*
, hence the compiler error.
Removing the const
from the argument would resolve the compiler error, as would making the key type an int const*
, but both these solutions would:
-
permit some other part of the program, with non-
const
access to aB
instance that was passed topush()
, to change a value of one of the keys within the set and break the set invariant:A a; B b1{17, 22}; B b2{30, 22}; a.push(b1); a.push(b2); b1.x = 30; // set no longer contains unique keys.
-
introduce a dependency of the
set
on the lifetime of the object referenced byb
:A a; a.push({14, 23}); // a now contains a dangling pointer.
The safest solution is to store an int
as the key, see http://ideone.com/KrykZw for online demo (thanks to bitmask for comment).
Possible solutions:
- Dynamically copy
b.x
. Or, - Use
int const*
as the key. Or preferably (avoiding explicit dynamic allocation), - Use
int
as the key, instead of anint*
(see http://ideone.com/KrykZw)