我想创建一个只使用一种指定数据类型的程序。我会根据收到的第一个数据,用 typeof 建立程序中所有数据的类型。
例如,在这种情况下:
int main() {
float a = 5;
typeof(a) var1;
typeof(b) var2;
//etc...
}
但是如果我想用库中包含的extern函数来做呢?
示例:
int main() {
void* var1 = create_var("hello"); //with the first call 'create_var()' establish the data type: in this case -> string
void* var2 = create_var("c"); //now i want that this is allowed
void* var3 = create_var(2); //and this is not allowed
我该怎么做这个功能?
答案 0 :(得分:0)
当您编写支持create_var
函数的框架时,您将发明一种新语言,它支持运行时类型检查(而不是C的编译/链接时类型检查)和模板。这就是C ++发生的事情(它最初是C的一组预处理器宏)。
从动态类型语言开始并将自己的类型强制添加到数据类型和函数可能更容易。
以前的工作示例: