Edit:
Please see my comments to 101010 and David's answer. Basically, I am wondering if it is possible to achieve the following goal in C++: forward declare a template class B, then use it as the type of the member data b of a class A, without (1) making A a template class and (2) caring about what special type will be used upon the time of the declaration of b.
What you are asking for makes no sense. It's not your fault. You are just misunderstanding how C++ works. Let me explain it to you.
Forward declaration
Example:
class Foo;
To the compiler the above statement means: "There will be a class named Foo defined somewhere else". From this point and until the it's definition Foo is an incomplete type. There are things you can do with an incomplete type and there are things you can't. In particular: you can't declare variables, and member variables (aka fields) of that type. Example:
class Foo;
class Bar0
{
Foo f; // syntax error: Foo is an incomplete type
};
void fun0(Foo f) // syntax error: Foo is an incomplete type
{
Foo f; // syntax error: Foo is an incomplete type
}
class Foo
{
int x;
Foo f; // syntax error: Foo is an incomplete type
void fun(Foo other) { // Ok here: see "note"
}
}; // Foo becomes complete here.
// Note: things are actually more complicated
// for example: Foo is complete inside it's own methods
// even if they are defined inside the definition of Foo.
class Bar1
{
Foo f; // Ok here: Foo is complete
};
void fun1(Foo f) // Ok here: Foo is complete
{
Foo f; // Ok here: Foo is complete
}
One of the things you can do with an incomplete type is declare a pointer to it. For example:
class Foo;
void fun(Foo* f) // Ok here
{
}
class Bar
{
Foo* f; // Ok here
};
Templates
Example:
template<class Bar>
class Foo
{
Bar b;
};
Template class is like a blueprint that can be used to create many classes. To create a class from a template you have to substitute it's arguments with concrete values. Foo<int>
and Foo<long>
are two separate types.
There are two things you can do:
To be continued
Further reading:
What is the difference between a definition and a declaration: https://stackoverflow.com/a/1410632/5420829
What can you do with an incomplete type:
https://stackoverflow.com/a/553869/5420829