第一個參數值,我有這個類C++得到模板參數包
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;
}
};
所有資產的第一個參數將始終的std :: string路徑。 我試圖得到這個值,看看它是否已經加載到列表中。 資產是一個抽象類。
class Asset
{
private:
std::string path;
public:
Asset(const std::string& path);
virtual ~Asset() = default;
virtual bool load() = 0;
std::string getPath();
};
類繼承的資產可能有不同數量的參數,因此我試圖捕捉到第一個參數的值,因爲它永遠是一個的std :: string路徑,你可以在資產類別看構造函數。
如果我告訴你只是在args之前聲明一個std :: string參數,假設它是強制性的,那麼我是否完全過於簡單? –
是的,那樣做。否則,你不能執行人們選擇通過的一切。爲什麼要留下做錯的可能性? –
建議'bool newAsset(Args && ... args)'正確支持完美轉發。沒有'&&',你總是在抄襲你的論點。 – aschepler