c++ - Generate a unique type or id for each template instantiation? ( example observer pattern ) -


is there way or technique generate unique types or ids each template instantiation @ compile time?

for example observer pattern:

#include <set> #include <iostream>  template <typename t> struct type2type {};    // maybe int2type   template<class t, class t_unique> struct observer_base {   virtual void notify ( t, type2type< t_unique > ) = 0; };  template<class t, class t_unique> struct subject_base {   // without t_unique parameter   typedef t_unique unique_type;    std::set< observer_base< t, unique_type >* > my_observer{};    void do_notify ()   {     ( auto obs : my_observer )       obs->notify ( t{}, type2type< unique_type >{} );   } };   class x {}; class y {};                                           // manual unique required? class subject_a : public subject_base< x, subject_a > {}; class subject_b : public subject_base< x, subject_b > {}; class subject_c : public subject_base< y, subject_c > {};  // typedef unique_. illustrate idea  typedef typename subject_a::unique_type unique_a; typedef typename subject_b::unique_type unique_b; typedef typename subject_c::unique_type unique_c;  class observer :   public observer_base< x, unique_a >,   public observer_base< x, unique_b >,   public observer_base< y, unique_c > {   virtual void notify ( x, type2type< unique_a > ) override   {     std::cout << "x subject_a" << std::endl;   }    virtual void notify ( x, type2type< unique_b > ) override   {     std::cout << "x subject_b" << std::endl;   }    virtual void notify ( y, type2type< unique_c > ) override   {     std::cout << "y subject_c" << std::endl;   } };  int main ( int argc, char **argv ) {   subject_a sub_a {};   subject_b sub_b {};   subject_c sub_c {};    observer obs {};    sub_a.my_observer.insert( &obs );   sub_b.my_observer.insert( &obs );   sub_c.my_observer.insert( &obs );    sub_a.do_notify();   sub_b.do_notify();   sub_c.do_notify(); } 

is there way in style ( without manual unique argument )? know sounds strange...

template<class t> struct subject_base {   typedef automatic_unique_type__or__what_ever unique_type; };  class subject_a : public subject_base< x > {}; class subject_b : public subject_base< x > {}; class subject_c : public subject_base< y > {}; 

i don't think there's way achieve asking for, it's possible achieve seem want. implementation doesn't need manual unique_type:

template<class t> class subject {   std::vector<std::function<void(t)>> observers;  public:   void do_notify () {     ( auto& obs : observers )       obs( t{} );   }    void add_listener(std::function<void(t)> l) {       observers.emplace_back(std::move(l));   } };  class x {}; class y {};  int main () {   subject<x> sub_a {};   subject<x> sub_b {};   subject<y> sub_c {};    sub_a.add_listener([](x){std::cout << "x subject_a" << std::endl;});   sub_b.add_listener([](x){std::cout << "x subject_b" << std::endl;});   sub_c.add_listener([](y){std::cout << "y subject_c" << std::endl;});    sub_a.do_notify();   sub_b.do_notify();   sub_c.do_notify(); } 

but it's substantially less oop-ey posted code. (live @ coliru)


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 -