c++ - Using pimpl with Templated Class and explicitly instantiated templates -


how use pimpl templated class, when explicitly instantiate templates?

all need example code.

what have tried is:

mytemplatedclass.h template< class t > class mytemplatedclass { private: class impl; impl* _pimpl;  public: void publicmethod(); } 

here implementation goes:

mytemplatedclass.cpp template< class t > class mytemplatedclass<t>::impl {     public:     void publicmethod(); }  template <class t> void mytemplatedclass<t>::impl::publicmethod() {     ... } 

forwarding method call implementation class:

template< class t > void mytemplatedclass<t>::publicmethod() {     _pimpl->publicmethod(); } 

explicit instantiation: example int , double:

template class mytemplatedclass< int >; template class mytemplatedclass< double; 

but doesn't seem work.

this answer question, doubt hoped achieve. suspect want declare template implementation outside scope of mytemplatedclass. might better design inherit template implementation instead of having member variable.

if compiler not support extern template declarations cannot see having template pointer implementation adds value. after have have implementation details wanted hide away in header file anyway.

#include <iostream>  template < class t > class mytemplatedclass { private:   template < class u> class impl {   public:      void implpublicmethod() {            std::cout << "standard implementation" << std::endl;            }   };    impl<t> * _pimpl; public:   mytemplatedclass() : _pimpl(new impl<t>) { }   ~mytemplatedclass() { delete _pimpl; }   void publicmethod() {      _pimpl->implpublicmethod();   } };  template<> class mytemplatedclass<int> { private:   class impl {   public:      void implpublicmethod() {           std::cout << "integer specialisation" << std::endl;      };  };   impl * _pimpl; public:   mytemplatedclass() : _pimpl(new impl) { }   ~mytemplatedclass() { delete _pimpl; }   void publicmethod() {      _pimpl->implpublicmethod();   } };  int main(int argc, char ** argv) {     mytemplatedclass<char> charversion;    charversion.publicmethod();     mytemplatedclass<int> intversion;    intversion.publicmethod();     return 0; } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -