Boost如何为类型选择创建地图? [英] Boost how to create a map for types selection?
问题描述
所以我使用BOOST.EXTENTION 加载模块.我有一个描述每个模块的特殊文件.我从该文件读取变量.
so i use BOOST.EXTENTION to load modules. I have a special file that describes each module. I read variables from that file.
所以例如: >
shared_library m("my_module_name");
// Call a function that returns an int and takes a float parameter.
int result = m.get<int, float>("function_name")(5.0f);
m.close();
对我来说会变成:
shared_library m("my_module_name");
// Call a function that returns an int and takes a float parameter.
int result = m.get<myMap["TYPE_1_IN_STRING_FORM"], myMap["TYPE_2_IN_STRING_FORM"]>("function_name")(5.0f);
m.close();
如何创建可以映射标准和服装类型的地图?
How to create such map that would map standard and costume types?
更新:
可能带有变体:
shared_library m("my_module_name");
int result = m.get<boost::variant< int, float, ... other types we want to support >, boost::variant< int, float, ... other types we want to support > >("function_name")(5.0f);
m.close();
可以暂停吗?所以只要在其中声明了所有想要的类型,我们就不会在意吗?
can halp? so we would not care as long as all types we want are declared in it?
推荐答案
为此,您将需要一个异构映射-也就是说,其元素可以具有不同的类型.此外,您将需要能够从函数中返回类型,而不仅仅是变量.
For that, you would need a heterogeneous map - that is, its elements can be of different types. Furthermore you would need the ability to return types from functions, not just variables.
现在,使用 Boost.Variant
或简单的 union
,但这将其绑定到编译时:我们需要知道每种可能创建该变体/联合的类型.
当然, Boost.Any
可以存储所有内容及其狗,但问题再次出现:您需要再次从该 Boost.Any
中提取 real 类型.问题重演.而且,如果您知道真正的类型,还可以制作一个变体/联合并为自己省下 any_cast
麻烦.
Now, a heterogeneous map would be possible with Boost.Variant
or a simple union
, but that binds it to compile time: we need to know every type that is possible to create that variant/union.
Of course a Boost.Any
would be possible to store everything and its dog, but the problem strikes again: you need to extract the real type out of that Boost.Any
again. The problem repeats itself. And if you know the real type, you can aswell just make a variant/union and save yourself the any_cast
trouble.
现在,对于另一件麻烦的事情:
Now, for another troublesome thing:
m.get<myMap["TYPE_1_IN_STRING_FORM"], myMap["TYPE_2_IN_STRING_FORM"]>
要使上述代码行有效,您需要C ++不具备的两个功能:返回类型和运行时模板的功能.让我们暂时忽略第一点.
模板是编译时,而 get
函数就是这样的模板.现在,要使用该模板,您的 myMap
必须能够在编译时返回类型,同时在运行时进行填充.看到矛盾了吗?这就是为什么需要运行时模板.
To make the above line work, you'd need two features that C++ doesn't have: the ability to return types and runtime templates. Lets ignore the first point for a moment.
Templates are compile-time, and the get
function is such a template. Now, to use that template, your myMap
would need to be able to return types at compile-time, while getting populated at runtime. See the contradiction? That's why runtime templates would be needed.
不幸的是,在C ++中运行时,这三件事完全不可能(或者非常困难,非常非常有限):异构数据类型(没有恒定大小),返回类型和模板.
涉及类型的所有内容都需要在编译时完成.@Gman的此博文与该问题有些相关.如果您想知道C ++ 不能做的事,那么绝对值得一读.
Sadly, exactly those three things are not possible (or extremely hard and very very limited) in C++ at runtime: heterogeneous data types (without constant size), returning types and templates.
Everything that involves types needs to be done at compile-time. This blogpost by @Gman somewhat correlates with that problem. It's definitly worth a read if you want to know what C++ just can't do.
因此,总结一下:您需要重新考虑并重构您的问题和解决方案.:|
So, to conclude: You'll need to rethink and refactor your problem and solution. :|
这篇关于Boost如何为类型选择创建地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!