交换机模板类型
Switch template type
我想为我的游戏创造一些存储空间。现在代码看起来像:
class WorldSettings
{
private:
std::map<std::string, int> mIntegerStorage;
std::map<std::string, float> mFloatStorage;
std::map<std::string, std::string> mStringStorage;
public:
template <typename T>
T Get(const std::string &key) const
{
// [?]
}
};
所以,我有一些关联容器来存储数据的确切类型。现在我想在设置中添加一些值:settings.Push<int>("WorldSize", 1000);
并得到它:settings.Get<int>("WorldSize");
。但如何切换需要映射由于传递类型到模板?
或者,也许你知道一个更好的方法,谢谢。
如果你的编译器支持这个1,你可以使用模板函数特化:
class WorldSettings
{
private:
std::map<std::string, int> mIntegerStorage;
std::map<std::string, float> mFloatStorage;
std::map<std::string, std::string> mStringStorage;
public:
template <typename T>
T Get(const std::string &key); // purposely left undefined
};
...
template<>
int WorldSettings::Get<int>(const std::string& key) {
return mIntegerStorage[key];
}
template<>
float WorldSettings::Get<float>(const std::string& key) {
return mFloatStorage[key];
}
// etc
注意方法不是const
,因为map<>::operator[]
不是const
。
同样,如果有人试图使用模板的类型不是你提供的特化类型,他们会得到链接器错误,所以你的代码不会出错或任何事情。
1如果没有,请参阅@gwiazdorrr的答案
首先,因为在c++ 11之前你不能专门化函数,你的成员函数必须在签名上不同——返回类型不算数。根据我在一些编译器上的经验,你可以不使用它,但像往常一样,你应该让你的代码尽可能接近标准。
也就是说,你可以添加一个不会影响性能和调用函数方式的虚拟参数:
public:
template <typename T>
T Get(const std::string &key) const
{
return GetInner(key, (T*)0);
}
private:
int GetInner(const std::string& key, int*) const
{
// return something from mIntegerStorage
}
float GetInner(const std::string& key, float*) const
{
// return something from mFloatStorage
}
以此类推。
Seth的答案是理想的,但是如果您无法访问c++ 11编译器,那么您可以使用模板类专门化来代替。它更冗长,但保持相同的功能。
class WorldSettings
{
template<class T>
struct Selector;
template<class T>
friend struct Selector;
private:
std::map<std::string, int> mIntegerStorage;
std::map<std::string, float> mFloatStorage;
std::map<std::string, std::string> mStringStorage;
public:
template <typename T>
T Get(const std::string &key)
{
return Selector<T>::Get(*this)[key];
}
};
template<>
struct WorldSettings::Selector<int>
{
static std::map<std::string, int> & Get(WorldSettings &settings)
{
return settings.mIntegerStorage;
}
};
template<>
struct WorldSettings::Selector<float>
{
static std::map<std::string, float> & Get(WorldSettings &settings)
{
return settings.mFloatStorage;
}
};
// etc.
在c++ 03中,我建议在容器类型中使用' boost::any '。您需要一个访问器:
std::map<std::string,boost::any> storage;
template <typename T> getValue( std::string const & key ) {
return boost::any_cast<T>( storage[key] );
}
这是一个粗略的草图,作为一个成员函数,它将是const,并且它应该使用' map::find '来在搜索时不修改容器,它应该处理无效的joy,并且可能将boost异常重新映射到您自己的应用程序异常中。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 交换机模板类型