在c++ /CLI中是否有等效于c#“var”关键字?

时间:2022-09-01 09:36:44

In C#, I like the var keyword for situations like this:

在c#中,我喜欢var关键字用于以下情况:

var myList = new List<MyType>();

Is there any equivalent in C++/CLI, or do I have to repeat the type name everytime just like this:

在c++ /CLI中是否有类似的东西,或者我是否必须像这样每次重复类型名:

List<MyType ^>^ myList = gcnew List<MyType ^>();

Could not find an explicit statement in the docs or by Google so far. I am using Visual Studio 2008.

到目前为止,还没有在文档或谷歌中找到明确的语句。我正在使用Visual Studio 2008。

4 个解决方案

#1


35  

In Visual Studio 2008 there is no such equivalent. However with Visual Studio 2010 you can use the auto keyword to implement var like semantics in C++. I know this works with non-managed C++ and I'm fairly certain it works for C++/CLI as well.

在Visual Studio 2008中没有这样的等效项。但是使用Visual Studio 2010,您可以使用auto关键字在c++中实现类似var的语义。我知道这是用非托管c++编写的,而且我很确定它也适用于c++ /CLI。

#2


12  

I know that type inference is envisioned in the C++1x standard:

我知道在c++ 1x标准中可以预见类型推断:

auto someStrangeCallableType = boost::bind(&SomeFunction, _2, _1, someObject);
auto otherVariable = 5;

Currently, AFAIK, there is no equivalent.

目前,AFAIK,没有类似的东西。

#3


4  

C++ has typedef. Just alias those hairy types with a typedef, and use the friendly name.

c++类型定义。只需将这些多毛的类型别名为typedef,并使用友好的名称。

No, there's no "var" keyword. Vaguely recall there's something to that effect in boost.

不,没有“var”关键字。模糊地回忆在boost中有某种作用。

#4


2  

C++0x is going to have an auto keyword: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1705.pdf

c++ 0x将有一个自动关键词:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1705.pdf

#1


35  

In Visual Studio 2008 there is no such equivalent. However with Visual Studio 2010 you can use the auto keyword to implement var like semantics in C++. I know this works with non-managed C++ and I'm fairly certain it works for C++/CLI as well.

在Visual Studio 2008中没有这样的等效项。但是使用Visual Studio 2010,您可以使用auto关键字在c++中实现类似var的语义。我知道这是用非托管c++编写的,而且我很确定它也适用于c++ /CLI。

#2


12  

I know that type inference is envisioned in the C++1x standard:

我知道在c++ 1x标准中可以预见类型推断:

auto someStrangeCallableType = boost::bind(&SomeFunction, _2, _1, someObject);
auto otherVariable = 5;

Currently, AFAIK, there is no equivalent.

目前,AFAIK,没有类似的东西。

#3


4  

C++ has typedef. Just alias those hairy types with a typedef, and use the friendly name.

c++类型定义。只需将这些多毛的类型别名为typedef,并使用友好的名称。

No, there's no "var" keyword. Vaguely recall there's something to that effect in boost.

不,没有“var”关键字。模糊地回忆在boost中有某种作用。

#4


2  

C++0x is going to have an auto keyword: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1705.pdf

c++ 0x将有一个自动关键词:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1705.pdf