In the following example the forward declaration the struct Y
foward declaration is not enough. If you comment out X::b, it compiles fine, since Y
has a full struct declaration to work with, but X
only has the forward declaration.
#include <functional>
#include <iostream>
struct Y;
struct X
{
std::function<bool(Y&)> b{[] (auto& y_) { return y_.a; }};
bool a{false};
};
struct Y
{
std::function<bool(X&)> b{[] (auto& x_) { return x_.a; }};
bool a{true};
};
int main()
{
return 0;
}
The following is the fix I could come up with:
#include <functional>
#include <iostream>
struct Y;
struct X
{
X();
std::function<bool(Y&)> b;
bool a{false};
};
struct Y
{
Y();
std::function<bool(X&)> b{[] (auto& x_) { return x_.a; }};
bool a{true};
};
X::X() : b([] (auto& y_) { return y_.a; }) {}
Y::Y() : b([] (auto& x_) { return x_.a; }) {}
int main()
{
return 0;
}
And while it works in this example, if the classes were polymorphic, this would require using X::X;
or using Y::Y;
in children of those classes respectively.
Is there a way to do this in the header files themselves?