这是我(可能有点奇怪)的想法,假设我想定义一个std::set对象来包含一些东西以供以后使用,但不幸的是我不确定哪个类型将std::set<not-sure>作为模板 arg 传递给,而这种not-sure类型将被确定通过一个字符串,像这样:
class X {
public:
foo()
{
char not_sure_type[20];
scanf("%s", not_sure_type);
if (strcmp(not_sure_type, "int"))
// then std::set<int>
else if (// "char")
// then std::set<char>
}
private:
void * _set;
};
这样,我可以确定它std::set<int>是否会被实例化,对吧?但是我怎么知道_set你应该指向 a std::set<int>?在不知道这一点的情况下,我也不能使用从to来回static_cast转换。_setvoid *std::set<int>*
那么我可以std::set<int>像数据成员一样保存以供以后使用吗?
任何想法都值得赞赏。