The question is self explanatory, but here's an example if desired:
Say I have a class 'Thing' with a private constructor, that is friends with a function 'make_thing':
class Thing
{
friend std::shared_ptr<Thing> make_thing();
Thing()
{
std::cout << "Thing constructor" << std::endl;
}
};
The 'make_thing' function has a struct defined inside it:
std::shared_ptr<Thing> make_thing()
{
// err: Thing::Thing() is private within make_shared
// auto thing = std::make_shared<Thing>();
// this is okay
struct AccessThing : public Thing {
AccessThing() : Thing() { }
};
auto thing = std::make_shared<AccessThing>();
return thing;
}
This compiles and works in gcc 4.8.2, clang 3.5 and msvc 2013. I can't call
make_shared<Thing>()
because Thing has a private constructor. However, I can get around this by creating a struct (AccessThing) with a public constructor that in turn calls Thing's private constructor, and then pass that to make_shared. To me this makes sense if AccessThing is a friend of Thing.
So what are the rules concerning how friendship is conferred to structs/classes defined within a friend function?