(First and foremost, sorry for my English)
In order to avoid the syntactic limitation of circular dependence, I want to know if the following proposed solution is really a solution or if it has more disadventages than adventages (let's me suposse that this mutual dependence is inavoidable):
Original situation:
class B;
class A
{
B needed;
};
class B {};
Output:
B has an incomplete type.
"Solution":
template<typename T = class B>
class tricky_A
{
static_assert(is_same<T, B>::value, "Too tricky!!");
T needed;
};
class B {};
using A = tricky_A<>;
int main()
{
A a; // At the instantiation point, B isn't an incomplete type.
}
Output:
No problems.
The common solution is to make use of pointers, but I see two problems when you really don't need them:
1) If you decide to retain pointer to other local objects, you should be sure (or the user of your class should be sure) that the life of your object is shorter than the life of the "retained" object.
2) If you decide to deal with dynamic memory, you should spend time reserving and destructing memory, and moreover it forces you to deal with exception handling, in order to make your code exception safe.
*) Longer code; smaller readability; harder mantenance; etc.
Should I use this "solution" or better to search other one?
EDIT: You are all right. This isn't a real circular dependence, so, there isn't anything to ask. This question can be closed.
EDIT2: A better question here.