简体   繁体   English

引用或指向不完整类型的std :: vector

[英]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.

相关问题 std :: vector的不完整类型 - Incomplete type for std::vector 传递std :: vector作为指针引用 - Passing a std::vector as a pointer reference 如何在不取消引用的情况下将指向不完整类型的指针转​​换为对不完整类型的引用 - How to convert a pointer to an incomplete type to a reference to an incomplete type with no dereferencing 使用不完整类型,引用vs指针无效 - Invalid use of incomplete type, reference vs pointer 对“std::vector”类型的 null 指针的引用绑定<int, std::allocator<int> &gt;"</int,> - Reference binding to null pointer of type "std::vector<int, std::allocator<int>>" 我的函数应该返回一个指向std :: vector的指针,还是对std :: vector的引用? - Should my function return a pointer to std::vector, or a reference to std::vector? Eigen3向量的指向std :: list的指针或引用 - Pointer or reference to std::list of Eigen3 vector 运行时错误:引用绑定到类型为“std::vector”的空指针<int, std::allocator<int> &gt;&#39; (stl_vector.h) - runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' (stl_vector.h) 具有类类型向量的前向声明-不允许指向不完整类类型的指针 - forward declaration with vector of class type - pointer to incomplete class type not allowed 合并间隔/第 1034 行:字符 9:运行时错误:引用绑定到类型为“std::vector”的空指针<int, std::allocator<int> &gt;&#39; - Merge Intervals/Line 1034: Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM