c++ - std::unique_ptr pimpl in dll generates C4251 with visual studio -
this not breaking issue clean code warnings getting on nerves.
i have been using c++11 version of pimpl idiom hide class implementation library usual way.
// 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>(); }
everything fine, keep getting warning:
warning c4251: 'framework::impl_': class 'std::unique_ptr<frameworkimpl,std::default_delete<_ty>>' needs have dll-interface used clients of class 'framework'
so tried have add:
template class export_api std::unique_ptr<frameworkimpl>;
before forward declaration warning change to:
warning c4251: 'std::_unique_ptr_base<_ty,_dx>::_mypair': class 'std::_compressed_pair<_dx,frameworkimpl *,true>' needs have dll-interface used clients of class 'std::_unique_ptr_base<_ty,_dx>'
i have being seeing issue since vs2010 , cannot figure way fix this. no problems on gcc or clang , break heart use old raw pointer version..
that common issue dll classes, use templates std
.
why happen?
reason simple: standard specifies guarantees, limitations , requirements. can sure, every c++ 11 compiler provide std::unique_ptr
, looks , works described on page. else implementation dependent.
the main problem is, different implementations may (and usually, will) use totally different structure particular types. use additional helper variables, different layout , on. may differ between 2 versions of same compiler. if client code touches in way member variables of class, need provide dll interface them. applies recursively types used dllexport
ed class.
you may want read this article on msdn, describes problem containers in mind.
this problem can simplified following:
- if client code has no access data, disable warning.
- if have members, intended use client code, create wrapper,
dllexport
ed or use additional indirectiondllexport
ed methods. - usually, can use pimpl hide non-dll types, in case not applicable, since use non-exportable type actually implement pimpl.
further reading:
Comments
Post a Comment