【发布时间】:2015-11-12 21:36:53
【问题描述】:
这不是一个重大问题,但我喜欢从警告中清除我的代码,所以这让我很紧张。
我一直在使用 c++11 版本的 pimpl idiom 以通常的方式隐藏我的库的类实现。
// dll header
class FrameworkImpl;
class EXPORT_API Framework
{
Framework(const Framework&) = delete;
Framework& operator=(const Framework&) = delete;
Framework(Framework&&) = delete;
Framework& operator=(Framework&&) = delete;
public:
Framework();
~Framework();
private:
std::unique_ptr<FrameworkImpl> impl_;
};
// application implementation
int main()
{
std::unique_ptr<Framework> test = std::make_unique<Framework>();
}
一切都会好起来的,但我会不断收到警告:
warning C4251: 'Framework::impl_': class 'std::unique_ptr<FrameworkImpl,std::default_delete<_Ty>>' needs to have dll-interface to be used by clients of class 'Framework'
所以我尝试了添加:
template class EXPORT_API std::unique_ptr<FrameworkImpl>;
在前向声明之前,但警告将更改为:
warning C4251: 'std::_Unique_ptr_base<_Ty,_Dx>::_Mypair': class 'std::_Compressed_pair<_Dx,FrameworkImpl *,true>' needs to have dll-interface to be used by clients of class 'std::_Unique_ptr_base<_Ty,_Dx>'
自 VS2010 以来我一直看到此问题,但我想不出解决此问题的好方法。 gcc 或 clang 没有问题,使用旧的原始指针版本会让我心碎..
【问题讨论】:
-
这个答案可能会有所帮助:stackoverflow.com/questions/24635255/…
-
作为测试,能不能把
std::unique_ptr<FrameworkImpl> impl_;换成struct do_nothing{ template<class T> void operator()(T&&)const{}; }; std::unique_ptr<FrameworkImpl, do_nothing> impl_;——我想看看是不是和默认删除器有关。 (如果这确实编译,您还没有完成!但它是诊断性的。)还要注意警告可能是虚假的。 -
@Yakk 相同的警告,但使用 'std::unique_ptr
' 代替
标签: c++ c++11 visual-c++ pimpl-idiom