Consider the following code:
class A
{
private:
struct B { private: int i; friend class A; };
public:
static void foo1()
{
B b;
b.i = 0;
}
static void foo2()
{
B b = {0};
}
};
Why does foo1 work but not foo2? Is not the struct initializer constructor visible for class A? Is there anyway to make this work in C++11?
(Note, removing the private makes foo2 working.)
Does not work because
Bis not an Aggregate. And it is not an Aggregate because it has an non-static private data member. If you remove the private specifier,Bbecomes an Aggregate and hence can be initialized in this manner.C++03 Standard 8.5.1 Aggregates
Para 7:
C++03 standard 8.5.1 §1: