使用unique_ptr作为不透明的对象ID

发布于 2025-01-30 15:35:14 字数 2260 浏览 3 评论 0原文

以下操作来隐藏我项目中第三方C ++图库(柠檬图库)的实现详细信息:

api.h file

class Node;
using NodeId = unique_ptr<Node>;
class ProgramGraph {
private:
  class ProgramGraphImpl;
  ProgramGraphImpl* pimpl;
public:
  ProgramGraph();
  NodeId add_instr_node(int opcode, const string& desc, BlockId& parent);
  ...
};

api.cpp文件

class Node {
public:
  ListDigraph::Node node;
  Node(ListDigraph::Node node) : node(node) {}
  ~Node() {}
};

class ProgramGraphImpl {
  NodeId add_instr_node(int opcode, const string& desc, BlockId& parent) {
    ListDigraph::Node n = pg.addNode(); // this is from the 3rd party library
    unique_ptr<Node> uq = make_unique<Node>(Node(n));
    node_map[n] = Instr(opcode, desc, parent); // Instr is another class defined in API.cpp
    return uq;
  }
  ...
};

NodeId ProgramGraph::add_instr_node(int opcode, const string& desc, BlockId& parent) {
  return pimpl->add_instr_node(opcode, desc, parent);
}

我在some_other_other_file.cpp中使用这样的api。

#include "API.h"
...
    const NodeId& currMI = pg.add_instr_node(opcode, opcode_desc, block_id);
...

我通过执行 ,我在下面收到错误(错误:“ sizeof”的应用程序不完整的类型program_graph :: node) - 有什么想法吗?谢谢。

/applications/xcode.app/contents/developer/platforms/macosx.platform/develform/sdks/macosx12.3.3.sdk/usr/incl/include/c ++ ++/v1/v1/v1/__ norement/soruse/simory_ptr.h:53:19:19:53:53:19::53:53:19::19::53:19::53:19 :: 53:53:19: 错误:将“ sizeof”应用于不完整类型的无效 'program_graph ::节点' static_assert(sizeof(_tp)&gt; 0, ^~~~~~~~~~~~ H:318:7: 注意:成员功能的实例化 'std :: default_delete&lt; program_graph :: node&gt; :: operator()' 在这里要求 _ ptr .second()(__ tmp); ^/applications/xcode.app/contents/developer/platforms/macosx.platform/developer/sdks/macosx12.3.sdk/usr/include/include/c+++++++++++++++++++++++++spr./__memory/unique_ptr.h:272:19:272:19:272:19:19: 注意:成员功能的实例化 'std :: simolor_ptr&lt; program_graph :: node&gt; :: reset'请求 这里〜unique_ptr(){reset(); } ^ some_other_file.cpp:151:36: 注意:成员功能的实例化 'std :: unique_ptr&lt; program_graph :: node&gt; :: 〜simel_ptr' 在这里要求 const nodeid&amp; currmi = pg.add_instr_node(opcode,opcode_desc,block_id);

I'm hiding the implementation details of third party C++ graph library (LEMON graph library) from my project by doing the following :

API.h file

class Node;
using NodeId = unique_ptr<Node>;
class ProgramGraph {
private:
  class ProgramGraphImpl;
  ProgramGraphImpl* pimpl;
public:
  ProgramGraph();
  NodeId add_instr_node(int opcode, const string& desc, BlockId& parent);
  ...
};

API.cpp file

class Node {
public:
  ListDigraph::Node node;
  Node(ListDigraph::Node node) : node(node) {}
  ~Node() {}
};

class ProgramGraphImpl {
  NodeId add_instr_node(int opcode, const string& desc, BlockId& parent) {
    ListDigraph::Node n = pg.addNode(); // this is from the 3rd party library
    unique_ptr<Node> uq = make_unique<Node>(Node(n));
    node_map[n] = Instr(opcode, desc, parent); // Instr is another class defined in API.cpp
    return uq;
  }
  ...
};

NodeId ProgramGraph::add_instr_node(int opcode, const string& desc, BlockId& parent) {
  return pimpl->add_instr_node(opcode, desc, parent);
}

I am using the API like this in some_other_file.cpp :

#include "API.h"
...
    const NodeId& currMI = pg.add_instr_node(opcode, opcode_desc, block_id);
...

When I compile it, I get the error below (error: invalid application of 'sizeof' to an incomplete type program_graph::Node) - any ideas ? Thank you.

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__memory/unique_ptr.h:53:19:
error: invalid application of 'sizeof' to an incomplete type
'program_graph::Node'
static_assert(sizeof(_Tp) > 0,
^~~~~~~~~~~ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__memory/unique_ptr.h:318:7:
note: in instantiation of member function
'std::default_delete<program_graph::Node>::operator()'
requested here
_ptr.second()(__tmp);
^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__memory/unique_ptr.h:272:19:
note: in instantiation of member function
'std::unique_ptr<program_graph::Node>::reset' requested
here ~unique_ptr() { reset(); }
^ some_other_file.cpp:151:36:
note: in instantiation of member function
'std::unique_ptr<program_graph::Node>::~unique_ptr'
requested here
const NodeId& currMI = pg.add_instr_node(opcode, opcode_desc, block_id);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2025-02-06 15:35:15

您是重新定义类programGraph在“ api.cpp”文件中。
尝试将其删除,重新编译并提供您的代码的更详细版本。
只是简单:

class Node {
public:
    Node()  {}
    ~Node() {}
};
NodeId ProgramGraph::add_instr_node(int opcode, const string& desc/*, BlockId& parent*/) {
    /*return pimpl->add_instr_node(opcode, desc, parent);*/
    return nullptr;
}

You was redefinition class ProgramGraph in "API.cpp" file.
Try removing it, and recompiling and provide a more detailed version of your code.
Just simple:

class Node {
public:
    Node()  {}
    ~Node() {}
};
NodeId ProgramGraph::add_instr_node(int opcode, const string& desc/*, BlockId& parent*/) {
    /*return pimpl->add_instr_node(opcode, desc, parent);*/
    return nullptr;
}
演出会有结束 2025-02-06 15:35:15

需要使用类似的自定义eleter:

using NodePtr = std::unique_ptr<Node,void(*)(Node*)>;

然后在.cpp文件中提供exter:

void delete_node(Node* node) { delete node; }
...
NodePtr uq{new Node(n), delete_node};

因为默认的deleter对不完整类型不起作用。

Need to use a custom deleter something like:

using NodePtr = std::unique_ptr<Node,void(*)(Node*)>;

and then providing the deleter in the .cpp file:

void delete_node(Node* node) { delete node; }
...
NodePtr uq{new Node(n), delete_node};

since the default deleter does not work on incomplete types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文