c++11 - C++ get the first argument value in template parameter pack -
i have class
class assetmanager { private: std::unordered_map<const std::type_info*, asset*> assets; public: assetmanager(); virtual ~assetmanager(); template <typename t, typename ...args> bool newasset(args... args) { //get path if(cached(path)) { return true; } auto asset = new t{ std::forward<args>(args)... }; assets[&typeid(*asset)] = asset; return *static_cast<t*>(assets[&typeid(t)]); } bool cached(const std::string& path) { for(auto asset : assets) { if(asset.second->getpath() == path) { return true; } } return false; } };
the first argument of every asset std::string path. i'm trying value , see if it's loaded in list. asset abstract class.
class asset { private: std::string path; public: asset(const std::string& path); virtual ~asset() = default; virtual bool load() = 0; std::string getpath(); };
classes inheriting asset may have different number of arguments , such i'm trying capture value of first argument because std::string path can see in asset class constructor.
if first argument std::string, makes lot of sense declare way. 1 thing, solves problem. another, ensures callers can never wrong. , if need in constructor of asset well, either pass separately constructor or declare there well.
template <typename t, typename ...args> bool newasset(const std::string &path, args&&... args) { //get path if(cached(path)) { return true; } auto asset = new t{ path, std::forward<args>(args)... }; assets[&typeid(*asset)] = asset; return *static_cast<t*>(assets[&typeid(t)]); }
i'm wondering use of typeid; absolutely using key in map correct? don't see operator<, , operator== not guaranteed give consistent results according http://en.cppreference.com/w/cpp/language/typeid (under 'notes'). might better use typeid.hash_code()
instead.
Comments
Post a Comment