D - What does an access modifier on a constructor actually do?

Viewed 53

Suppose I have the following code:

class Foo
{
    private this(int x){ }
}
void
main()
{
    auto f = new Foo(4);
}

To my astonishment, this actually compiles. I would expect a private constructor to be unusable outside of the class it's defined in, just like a method would, but that's clearly not the case. The language reference doesn't even mention access modifiers in the constructors' section.

So the question is twofold: what does an access modifier do when applied to a constructor (if it does anything at all), and how do I hide a ctor?

1 Answers
Related