I cannot use reference to incomplate (forward declared) class as template for list. But pointer works fine. For what I've read incomplete types are allowed for vector, list and forward list (and not allowed for map, set and others)
Here is example
#include <list>
#include <vector>
class MyClass;
void foo(const MyClass&); //works fine
std::vector<const MyClass&> ref_vec; //error
std::list<const MyClass&> ref_list; //error
std::vector<const MyClass*> p_vec; //works fine
std::list<const MyClass*> p_list; //works fine
class MyClass
{};
void main(){}
The problem here does not have anything to do with forward declaration. You can see this by changing the program to this:
#include <list>
#include <vector>
class MyClass
{};
void foo(const MyClass&); //works fine
std::vector<const MyClass&> ref_vec; //error
std::list<const MyClass&> ref_list; //error
std::vector<const MyClass*> p_vec; //works fine
std::list<const MyClass*> p_list; //works fine
int main(){}
The lines marked as //error
and /works fine
will fail in the exact same manner. The problem is that std::vector
and std::list
are not allowed to have a type which is a reference. The reason for this in C++11 and beyond is that elements must be Erasable, which references are not (more details are in the question Why can't I make a vector of references?).