简体   繁体   English

C++中的动态类型

[英]Dynamic type in C++

Is it possible to determine the data-type to use during runtime?是否可以确定在运行时使用的数据类型? Let's consider an example code like this:让我们考虑这样的示例代码:

void myFunction(type) {

    new std::unordered_map<type, type> umap;
}
...

I'm aware that I can make use of std::any to support multi-types but I just want to know if there's any way to do the former.我知道我可以使用std::any来支持多类型,但我只想知道是否有任何方法可以做到前者。

Thanks!谢谢!

yes you can by using templates with C++ , for quick example :是的,您可以将模板与 C++ 一起使用,例如:

template<typename genericType>
void myFunction(genericType variable) {
   unordered_map<genericType, genericType> umap;
}

ps: i don't think you need to use new keyword here. ps:我认为您不需要在这里使用new关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM