似乎我无法传递int
参数给Variant
函数参数.如何解决?
只需清楚
告诉编译器
你想要什么即可.
cpp
import std;
void f(Variant x) {
writeln(x);
}
void main() {
f(Variant(42));
f(Variant(2.5));
f(Variant("Hi!"));
}
给它传递个带someFunction(Variant(myInt))
的Variant
.
或编写仅限于非变体
的模板,来完成上述操作.
cpp
auto someFunction(T)(T t)
if (!is(T : Variant))
{
return someFunction(Variant(t));
}
auto someFunction(Variant v)
{
//...
}
void main()
{
someFunction(42);
someFunction("hello");
someFunction(3.14f);
someFunction(true);
someFunction(Variant(9001));
}