第一个参数值,我有这个类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