Link or pointer to std :: vector of incomplete type

As indicated here: How can I use an incomplete type as a template parameter for a vector here? using an incomplete type as a template argument when instantiating a template component in undefined mode. But is this rule true if we only have a pointer / link to a template component with an incomplete type as an argument? In this case, does stagnation also occur?

For instance:

// SomeAlgoInterface.hpp #include <vector> struct Result; // forward declaration class SomeAlgoInterface { public: virtual ~SomeAlgoInterface() = default; public: // the following line is definitely OK, ... virtual void f1(const Result & result) = 0; // ... but I'm not quite sure about the following one virtual void f2(const std::vector<Result> & results) = 0; }; 

In other words, is the code above valid or not?

+6
source share
1 answer

The declaration is true if you do not call f2 .

The compiler should not know the inner Result class unless you call f2 . The ad has no storage allocation.

If any compilation module calls f2 , you need to provide the full type for the Result class or you need another reference parameter to call f2 :

 void another_f(SomeAlgoInterface& i, std::vector<Result>& results) { i.f2(results); } 
+2
source

All Articles