views:

391

answers:

4

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

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:

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.

+3  A: 

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

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

Seva Alekseyev
Well, typedef may help sometimes, but, for example, a "typedef List<MyType^>^ ListMyType" does not gain much.
Doc Brown
You could typedef it to something shorter, like just `list`. It doesn't have to be a globally unique name, just unique in the context in which it is used.
jalf
+6  A: 

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

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

Currently, AFAIK, there is no equivalent.

Yacoder
I lol'ed so much when I say C++1x...
Martinho Fernandes
the *code* *name* of the next standard is still c++0x, even if we have 2010 already.
sellibitze
+2  A: 

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

rmn
<nitpick> auto is already a keyword. they just recycled it. </nitpick>
sellibitze
+10  A: 

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.

JaredPar
This is a feature in C++0x, is it not?
John Dibling
Yes, this is a feature of C++0x.
DaMacc
I'm not 100% sure, but I seem to recall that C++0x features would *not* get ported to C++/CLI at this point. They just don't have the resources atm. In the long run, it'll definitely be added to C++/CLI, but I don't think it'll be in VS2010. I could be wrong though, this is just what I recall reading on the VCTeam blog.
jalf
It's C++1x already, happy New Year! =)
Yacoder
No it's not, because C++0x sounds better, and has the advantage that everyone is used to the name, and knows what it refers to.
jalf
@Yacoder: in addition to what jalf said, we can also move to hex and call it C++0A if it gets approved this year
sbk
@sbk: good idea, but then it should be C++0xA =)))
Yacoder
@jalf: 0x vs 1x... Now the committee is going to discuss that question for about one-two more years =)
Yacoder