c++ - Can this be done with static typing? -



c++ - Can this be done with static typing? -

this method attempts select (std::vector<?>) based on key (std::string), ? either int or float:

template<typename l> inline void ensembleclustering::graph::fornodeswithattribute(std::string attrkey, l handle) { // nodemap attrkey auto nodemap; // ? auto findidpair = this->attrkey2idpair.find(attrkey); if (findidpair != this->attrkey2idpair.end()) { std::pair<index, index> idpair = findidpair->second; index typeid = idpair.first; index mapid = idpair.second; // nodemaps in vector, 1 each node attribute type int, float, nodeattribute switch (typeid) { case 0: nodemap = this->nodemapsint[mapid]; break; case 1: nodemap = this->nodemapsfloat[mapid]; break; } // iterate on nodes , phone call handler attribute this->fornodes([&](node u) { auto attr = nodemap[u]; handle(u, attr); }); } else { throw std::runtime_error("node attribute not found"); } }

the relevant fellow member of class are:

std::map<std::string, std::pair<index, index>> attrkey2idpair; // attribute key -> (attribute type index, attribute map index) // storage std::vector<std::vector<int> > nodemapsint; // has type id 0 std::vector<std::vector<float> > nodemapsfloat; // has type id 1

this not compile because auto nodemap (= std::vector<?>) not initialized. in order initialize it, have know type @ compile time.

maybe attempting cannot done static typing. there c++ way accomplish this?

the fact these templates has nil it. std::vector<std::vector<int> > , std::vector<std::vector<float> > 2 totally unrelated classes, , behave such. if need this, you'll have define abstract base of operations class , 2 derived classes, each of wraps corresponding std::vector. don't see how you'll able utilize it, or define appropriate abstract base of operations class, because type contained in vector permeates interface. types utilize in every phone call have different.

c++ c++11 static-typing dynamic-typing

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -