如何从dll正确返回std :: list [英] how to correctly return std::list from dll

查看:178
本文介绍了如何从dll正确返回std :: list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该项目具有以下结构:
1. Dll - 具有核心逻辑和类层次结构
2. exe - 控制台应用程序处理命令行并启动算法
3. Dll - 像一个单元测试工具包硬编码填充从第一个Dll的集合,并将集合传递到exe控制台处理

the project has this structure: 1. Dll - with core logic and class hierarchy 2. exe - console app which processes commandline and starts the algorithms 3. Dll - tests, like a unit test kit - hardcoded filling up the collection of the ojects from first Dll and passes the collection to exe console to process

所以Dll点3应该返回集合例如std :: list),集合包含多态对象,然后应该存储指针
我更喜欢使用std :: unique_ptr而不是原始指针

so Dll point 3 should return the collection (e.g. std::list), the collection contains polymorphic objects, then should store the pointer, I'd prefer to use std::unique_ptr instead of raw pointers

我看到unique_ptr只支持移动语义,我使用emplace_back成员填充列表。
但是有一个问题从与DLL相关的Dll返回std :: list>集合与MSVC类导出技术

I see that unique_ptr supports move semantic only and I'm filling up the list using emplace_back member. however there is a problem to return std::list> collection from Dll related with MSVC class exporting technique

如果我理解这一点:$ b​​ $ b在dll和exe之间共享的项目头应该包含这样的内容,EXP_DLL应该为Dll定义,未定义为exe

if I understand this right : shared between dll and exe projects header should contain something like this where EXP_DLL should be defined for Dll and undefined for exe

 #ifdef EXP_STL
 #    define DECLSPECIFIER __declspec(dllexport)
 #    define EXPIMP_TEMPLATE
 #else
 #    define DECLSPECIFIER __declspec(dllimport)
 #    define EXPIMP_TEMPLATE extern
 #endif

 EXPIMP_TEMPLATE template class DECLSPECIFIER std::list<std::unique_ptr<MyBassClass>>;

 std::list<std::unique_ptr<MyBassClass>> DECLSPECIFIER make_test_array();

此定义:

 EXPIMP_TEMPLATE template class DECLSPECIFIER std::list<std::unique_ptr<MyBassClass>>;

引发错误


C2280:std :: unique_ptr> :: unique_ptr(const
std :: unique_ptr< _Ty,std :: default_delete< _Ty >>&)':尝试
引用已删除的函数

C2280:std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function

我看到尝试调用unique_ptr的复制ctor(当然已删除)

I see the attempt to call copying ctor of unique_ptr (of course, deleted)

您可以为我澄清这些问题:

could you clarify these questions for me:


  1. 此实例化模板的导出声明如何在此处呼叫复制ctor? >
  2. 你能建议解决方案如何避免这个?


推荐答案

假设符合以下条件:


  1. 解决方案中的所有项目链接相同的运行时DLL(/ MD选项)

  2. 类MyBaseClass归因于DECLSPECIFIER。

  3. 类MyBaseClass在make_test_array声明之前完全定义。

您不能导出模板特化。只需删除 EXPIMP_TEMPLATE模板类... 行即可。
std :: list和std :: unique_ptr代码将被内联。

you must not export template specialization at all. Just remove the line EXPIMP_TEMPLATE template class .... std::list and std::unique_ptr code will be inlined. You will get warning C4251, ignore it.

当您尝试导出 std :: list 专用化,全部 std :: list< T> 方法。其中一些(例如赋值运算符)要求T是可复制的。这就是为什么使用unique_ptr的专门化无法编译的原因。

When you try to export std::list specialization, all std::list<T> methods are instantiated. Some of them (e.g. assignment operator) require T to be copyable. That's why specialization with unique_ptr can't be compiled.

这篇关于如何从dll正确返回std :: list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆