从void Pointer(创建通用存储)铸造有什么问题
Is there something wrong in casting from void pointer (to create a generic storage)?
我正在尝试在C 中创建一个通用存储类。如果您查看Code Bellow,我想存储string
/AnyType
的地图并访问它们。
class StoresTestC
{
public:
template < class SettingsType >
std::map< std::string, SettingsType >* getStore(std::string const&);
private:
std::map< std::string, void* > stores_;
};
template < class SettingsType >
std::map< std::string, SettingsType >* StoresTestC::getStore(std::string const& name)
{
if (stores_.count(name) > 0)
{
void* temp = this->stores_[name];
return (std::map< std::string, SettingsType >*)(temp);
}
else
{
std::map< std::string, SettingsType > * result = new std::map< std::string, SettingsType > ();
this->stores_[name] = result;
return result;
}
}
我看到了这两个明显的危险:
如果我以错误的
SettingsType
/name
称呼它,我将据我所知(我可能是错)的错误铸件,将导致不确定的行为。它将创建一个内存泄漏,但是我有一个解决方案(在这里也披露了两个很久了)。
还有其他可能出错的东西,您可以预见吗?
首先退后一步,并确保您真正想要做到这一点。然后再看一次设计。
好吧,您仍然需要此功能?
使用std::map<std::string, boost::any>
,其中boost::any
始终是map
类型。然后,当您使用any_cast
或任何机制将物品撤回时,您可以保证它是正确的类型或抛出的,因此您永远不会冒险不确定的行为。此外,由于any
是按值,您也没有可能在周围漂浮的内存泄漏。
我还应该注意,在您的原始解决方案中,如果您使用shared_ptr<void*>
,它将记住如何删除该shared_ptr
中存储的原始类型,从而删除您提到的内存泄漏。
编辑:我看不到其他明显的技术问题。但是,请注意,具有这样的地图是可能的/可能导致认知(" Grokking")问题的问题,并且确实会增加代码的复杂性。
是的,您对点1的怀疑是对的(如您所提到的,应该可以解决第2点,但是请考虑所包含的地图的破坏者,应正确调用)。
我不会在不提供客户端加强 SettingsType
和 std::string
'typeName'表示中的简单机制的情况下将这种接口放入野外。一个简单的错别字会弄乱整个事情。我认为boost::any
会在您尝试将值投入所需/预期结果类型的情况下表达这一点。
为什么在这里完全需要std::string
表示?如果使用RTTI,则可以使用typeid()
。
stores_
的密钥,因为您必须将类型作为模板参数传递以获取地图,这是否只能每类映射?
?namespace StoresTest {
template<typename T>
struct MapStruct {
static std::map<std::string, T> the_map;
};
template<typename T> std::map<std::string, T> MapStruct<T>::the_map;
template<typename T>
inline std::map<std::string, T>& getStore()
{ return MapStruct<T>::the_map; }
}
然后,您可以通过执行
获得带有Value类型Foo
的地图 std::map<std::string, Foo>& foo_map = StoresTest::getStore<Foo>();
您不再需要string
来命名类型,但是如果您有其他理由需要它。
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- c++无值sort()的问题是什么?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 基本的 c++ 问题:如果我在函数中创建某些内容并返回它会发生什么?
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 什么是钻石问题?是一系列问题还是特定问题?
- 格式说明符C++有什么问题
- 我应该在 main 函数中写什么来测试我的问题?
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 方法问题 - 什么会改变值,什么不会改变?什么是无效的?