【发布时间】:2021-03-24 14:53:10
【问题描述】:
我是 C++ 新手,需要将类型的字符串表示形式转换为类型本身,如下所示:
"int" 到 int"float" 到 float
等等……
但是,实现它似乎非常困难。例如,在伪代码中:
//how to implement this function or something like this...
auto gettype(string typestr);
//usage
string types[4] = {"int", "double", "float", "string", ...};
gettype(type[0]) val; //then the type of val is int
【问题讨论】:
-
如果你假设要让它工作,你打算如何初始化
val? “需要将类型的字符串表示形式转换为类型本身”——我觉得这不太可能。 actual problem 是什么? -
为什么需要这个?您要解决什么实际问题?
-
一个函数只能有一个返回类型。仅仅因为它被声明为
auto并不意味着它可以根据输入而改变。我想你有一个xy-problem -
@Patrick Roberts 假设它的值也是从字符串转换而来的
-
你想写解释器吗? ChaiScript 之类的东西?