[英]Reference or pointer to std::vector of incomplete type
As answered here: How can an incomplete type be used as a template parameter to vector here? 正如在这里所回答的: 如何将不完整类型用作向量的模板参数? usage of incomplete type as template argument when instantiating a template component can result in undefined behaviour.
在实例化模板组件时使用不完整类型作为模板参数可能导致未定义的行为。 But does that rule hold true when we have only pointer/reference to template component with incomplete type as argument?
但是当我们只有指向不完全类型的模板组件的指针/引用作为参数时,该规则是否成立? Does the instatiation happen in this case too?
在这种情况下,实例也会发生吗?
For example: 例如:
// 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? 换句话说,上面的代码是否有效?
The declaration is correct, so long as you don't call f2
. 声明是正确的,只要你不打电话给
f2
。
The compiler doesn't need to know the internal of Result
class if you don't call f2
. 如果不调用
f2
,编译器不需要知道Result
类的内部。 There's no storage allocation in the declaration. 声明中没有存储分配。
If some compilation unit call f2
, you need to supply the complete type for Result
class, or you need another reference parameter to call f2
: 如果某个编译单元调用
f2
,则需要提供Result
类的完整类型,或者需要另一个引用参数来调用f2
:
void another_f(SomeAlgoInterface& i, std::vector<Result>& results)
{
i.f2(results);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.