1

Consider the following code:

struct A{};

int main()
{
    std::set<A*> aset;
    aset.emplace();
    std::cout << aset.size() << std::endl;   //prints "1"
    return 0;
}

DEMO

Why does the empty emplace() adds an element to the set of pointers?

davidhigh
  • 14,652
  • 2
  • 44
  • 75

1 Answers1

3

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.

Barry
  • 286,269
  • 29
  • 621
  • 977