Will VC++ 2005 implement switch with case int::typeid in the final release?

N

nobody

With Visual C++ 2005 beta 2, I get the below compling error for the
following code. I think this error is not acceptable to me because
int::typeid is a constant and is known to compiler when compiling. Will VC++
2005 implement switch with case int::typeid in the final release?

error C2450: switch expression of type 'System::Type ^' is illegal

1> No user-defined-conversion operator available, or

1> Integral expression required

1>.\sproadapter.cpp(271) : error C2051: case expression not constant



Type ^t = obData->GetType();

switch (t)

{

case int::typeid:

break;

default:

break;

}
 
L

Lloyd Dupont

I believe that C++ doesn't care about constantness, it just takes only int
(or type with implicit cast to int) argument.
 
C

Carl Daniel [VC++ MVP]

nobody said:
With Visual C++ 2005 beta 2, I get the below compling error for the
following code. I think this error is not acceptable to me because
int::typeid is a constant and is known to compiler when compiling.
Will VC++ 2005 implement switch with case int::typeid in the final
release?

No.

System::Type is not an integer type and therefore cannot be used as the type
of a switch/case regardless of real or apparent const-ness.

-cd
 
N

nobody

Consider the following code:

Type ^t = obData->GetType();

if(t == int::typeid)

{

//do something

}

else if(t == __int64::typeid)

{

Type ^t = obData->GetType();

}

else if(t == double::typeid)

{

Type ^t = obData->GetType();

}

else if(t == float::typeid)

{

Type ^t = obData->GetType();

}

else

{

}



If I can use switch, it will much better. Do you know any method to use
switch?
 
T

Tomas Restrepo \(MVP\)

Consider the following code:
If I can use switch, it will much better. Do you know any method to use
switch?

No, you can't. Depending on what you're doing you might be able to implement
a table driven approach, but that's about it.
 
B

Brandon Bray [MSFT]

nobody said:
If I can use switch, it will much better. Do you know any method to use
switch?

While everyone else is correct that this is not supported by the C++
language, you can request that it be supported (see link to feedback below).
This request is very similar to another feature which we do not support --
switch on strings. C# supports switch on strings. The code generated in both
your case and the string case would effectively be a series of if-then-else
statements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top