Consider the following code:
struct A{};
int main()
{
std::set<A*> aset;
aset.emplace();
std::cout << aset.size() << std::endl; //prints "1"
return 0;
}
Why does the empty emplace()
adds an element to the set of pointers?
Consider the following code:
struct A{};
int main()
{
std::set<A*> aset;
aset.emplace();
std::cout << aset.size() << std::endl; //prints "1"
return 0;
}
Why does the empty emplace()
adds an element to the set of pointers?
Because emplace
will:
[Insert] a new element into the container by constructing it in-place with the given args if there is no element with the key in the container.
The container was previously empty, so you're definitely going to insert a new element. Zero arguments is a valid constructor for A*
, so the code compiles and you end up with a set
with one, value-initialized pointer to A
.