c++ - template enable if is pointer -


i try make class manage resources (resourcemanager).

for use template c++11.

here's do:


template<class k,class t> class resourcemanager {     public:         resourcemanager();         ~resourcemanager();          /* code */          void clear();      private :          std::unordered_map<k,t> resource;          template <bool b>         void clear(); }; 

 template<class k,class t>  void resourcemanager<k,t>::clear()  {     clear<std::is_pointer<t>::value>();  };   template<class k,class t>  template<bool b>  void resourcemanager<k,t>::clear<b>()  {     for(auto& x:resource)     delete x.second;     resource.clear();  }   template<class k,class t>  template<>  void resourcemanager<k,t>::clear<false>()  {     resource.clear();  } 

in short, try have different comportement if t pointer (auto delete).

i tried use std::enable_if, did not understand how functioned, , if right way take.

if me...


code can found here: https://github.com/krozark/resourcemanager

you use solution based on overload , tag dispatching. clear() member function defined way:

void clear() {     do_clear(std::is_pointer<t>()); } 

and class template include 2 overloads of do_clear(), follows:

template<class k,class t> class resourcemanager {      // ...  private:      void do_clear(std::true_type);     void do_clear(std::false_type);  }; 

and here definition of 2 member functions:

template<class k, class t> void resourcemanager<k, t>::do_clear(std::true_type) {     for(auto& x:resource)     delete x.second;     resource.clear(); }  template<class k, class t> void resourcemanager<k, t>::do_clear(std::false_type) {     resource.clear(); } 

notice, however, have option of using smart pointers , other raii resource wrappers avoid calling delete explicitly on raw pointers.


Comments

Popular posts from this blog

Pull out data related to my apps from Android Play Store and iOS App Store -

Change php variable from jquery value using ajax (same page) -

How can I fetch data from a web server in an android application? -