c++ - Is there any generic method for smart pointers and raw pointers? -
i have question c++ template.
template <class container> void trytriggers(const container& entities) { (container::const_iterator ent = std::begin(entities);ent != std::end(entities); ++ent) { if ((*ent)->isreadyfortriggerupdate() && (*ent)->isalive()) { (triggerlist::const_iterator trg = std::begin(_triggers);trg != std::end(_triggers); ++trg) { //try(..) method takes *entity(entity's pointer) parameters. //but container implemented unique_ptr. //ex) vector<unique_ptr<entity>> v; //so used get() method acquire raw pointer. (*trg)->try((*ent).get()); } } } } the code template member function containers.
it works class witch have isreadyfortriggerupdate() , isalive() in it.
try(..) method takes *entity(entity's pointer) parameters.
in case, use unique_ptr int containers have use get() method raw pointer.
the function doesn't make problems but.. if use container raw pointers
vector <entity*> v; then make problem.
question: want make more generic can works raw pointers , smart pointers. there solution this?
in stl, there std::begin(con) solve problems related iterating problem in con.begin(). expect generic method
std::begin(con) for above problem.
asides @slava's quite correct answer, can, in general, write template function , specialize raw pointer:
template<class t> auto find_address_of(t &&p) -> decltype(p.get()) { return p.get(); } template<typename t> t *find_address_of(t *p) { return p; }
Comments
Post a Comment