开发者

Is there a var type equivalent in C++?

开发者 https://www.devze.com 2022-12-31 02:20 出处:网络
So I know that C++ is strongly typed and was just wondering if there was any library (or any thing for that fact of the matter) that would allow you to make a variable that has no initial specific typ

So I know that C++ is strongly typed and was just wondering if there was any library (or any thing for that fact of the matter) that would allow you to make a variable that has no initial specific type开发者_如何学运维 like var in Python.


Take a look at boost::any and boost::variant.


Two main things come to mind:

  1. BOOST_AUTO
  2. C++0x type inference


In the new C++11, there is the auto keyword which no longer refers to automatic duration but rather type inference occurs. So you can do this:

auto index = 1; // index is inferred to be int
auto c = 'a' // c is inferred to be a char
0

精彩评论

暂无评论...
验证码 换一张
取 消