c++ - How to store type information, gathered from a constructor, at the class level to use in casting -


i trying write class can store , use type information in without need template parameter.

i want write this:

class example { public:     template<typename t>     example(t* ptr)         : ptr(ptr)     {         // typedef t enclosedtype; want avaialable @ class level.     }      void operator()()     {         if(ptr == null)             return;          (*(enclosedtype*)ptr)(); // can cast pointer , call () operator if class has one.     }  private:     void* ptr; } 

i not asking how write is_functor() class.

i want know how type information in constructor , store @ class level. if impossible, different solution appreciated.

i consider , valid question, however, there no general solution beside using template parameter @ class level. tried achieve in question -- using typedef inside function , access in whole class -- not possible.

type erasure

only if impose restrictions onto constructor parameters, there alternatives. in respect, here example of type erasure operator() of given object stored inside std::function<void()> variable.

struct {     template<typename t>     a(t const& t) : f (std::bind(&t::operator(), t)) {}      void operator()() const     {         f();     }      std::function<void()> f; };  struct b {     void operator()() const     {         std::cout<<"hello"<<std::endl;     } };  int main() {     a(b{}).operator()();  //prints "hello" } 

demo

note, however, assumptions underlying approach: 1 assumes passed objects have operator of given signature (here void operator()) stored inside std::function<void()> (with respect storing member-function, see here).

inheritance

in sense, type erasure "inheriting without base class" -- 1 instead use common base class constructor parameter classes virtual bracket operator, , pass base class pointer constructor.

struct a_parameter_base {     void operator()() const = 0; };  struct b : public a_parameter_base {     void operator()() const { std::cout<<"hello"<<std::endl; }            };  struct {     a(std::shared_ptr<a_parameter_base> _p) : p(_p) {}      void operator()()     {         p->operator();     }      std::shared_ptr<a_parameter_base> p; } 

that similar code in question, not use void-pointer pointer specific base class.

both approaches, type erasure , inheritance, similar in applications, type erasure might more convenient 1 gets rid of common base class. however, inheritance approach has further advantage can restore original object via multiple dispatch

this shows limitations of both approaches. if operator not void instead return unknown varying type, cannot use above approach have use templates. inheritance parallel is: cannot have virtual function template.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -