Type Variable
-
almost: switch(sizeof(type)) { case sizeof(short): //need pre-defined break; includeh10
In most 32 bit C++ compilers, float and int are both sizeof 4, so how's that going to work? -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
-
Hi, I am in need of a variable that can hold a type (variable-type ex: int or double) and not a value of the specific type, just the type. Something that works like this:
VARTYPE Type;
Type = int; // The type is integer.
switch (Type) {
case short: // The type is short.
//...
break;
case int: // The type is integer.
//...
break;
case float: // The type is float.
//...
break;
case double: // The type is double.
//...
break;
}I know I can simply declare a bunch of constant numbers, who each represents a variable type. But that would be very non-optimal if there already exists an easier way. Could I use templates to achieve this or the use of preprocessors/macros? Just to be clear, I am looking for a variable that only contains a type and not a value. Any help or tips are most appreciated. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
Off the top of my head, I believe some of Andrei Alexandrescu's work (Loki??) covers situations like this, although I don't have a reference to hand. Also, the VARIANT struct already has some associated defines that could be used as a base for this - eg VT_I4, VT_UI1, VT_BSTR, etc... -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
-
Hi, I am in need of a variable that can hold a type (variable-type ex: int or double) and not a value of the specific type, just the type. Something that works like this:
VARTYPE Type;
Type = int; // The type is integer.
switch (Type) {
case short: // The type is short.
//...
break;
case int: // The type is integer.
//...
break;
case float: // The type is float.
//...
break;
case double: // The type is double.
//...
break;
}I know I can simply declare a bunch of constant numbers, who each represents a variable type. But that would be very non-optimal if there already exists an easier way. Could I use templates to achieve this or the use of preprocessors/macros? Just to be clear, I am looking for a variable that only contains a type and not a value. Any help or tips are most appreciated. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
Look up
typeid
and thetype_info
struct --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 released -
Look up
typeid
and thetype_info
struct --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 releasedI forgot about those :-O Obviously been too long since I did any major work in C++. Of course, IIRC, typeid and type_info are platform dependant, so code isn't necessarily portable, right? /goes into a bunker with a C++ compiler and a copy of Stroustrup. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
-
I forgot about those :-O Obviously been too long since I did any major work in C++. Of course, IIRC, typeid and type_info are platform dependant, so code isn't necessarily portable, right? /goes into a bunker with a C++ compiler and a copy of Stroustrup. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
Ian Darling wrote: Of course, IIRC, typeid and type_info are platform dependant, so code isn't necessarily portable, right? Not sure what you mean... Comparing
type_info
s generated on two different platforms probably won't work, if that's what you had in mind. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 released -
Ian Darling wrote: Of course, IIRC, typeid and type_info are platform dependant, so code isn't necessarily portable, right? Not sure what you mean... Comparing
type_info
s generated on two different platforms probably won't work, if that's what you had in mind. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 releasedMichael Dunn wrote: Not sure what you mean... Comparing type_infos generated on two different platforms probably won't work, if that's what you had in mind. Nope. Just that you couldn't rely on certain things about the type_info implementation always being the same across platforms, for example, what typeinfo::name() returns. My reading of "Design and Evolution of C++" suggests that there is scope for differing definitions of the type_info class on different C++ platforms. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
-
Michael Dunn wrote: Not sure what you mean... Comparing type_infos generated on two different platforms probably won't work, if that's what you had in mind. Nope. Just that you couldn't rely on certain things about the type_info implementation always being the same across platforms, for example, what typeinfo::name() returns. My reading of "Design and Evolution of C++" suggests that there is scope for differing definitions of the type_info class on different C++ platforms. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
Ok, I see. Honestly, I've never used
type_info
so I'm just repeating the docs here. ;) --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 released -
Ok, I see. Honestly, I've never used
type_info
so I'm just repeating the docs here. ;) --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 releasedThanks! Althougth I am not quite sure if I might be able to use it as I hoped, it is still a better alternative then declaring your own Type-Ids. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
-
Hi, I am in need of a variable that can hold a type (variable-type ex: int or double) and not a value of the specific type, just the type. Something that works like this:
VARTYPE Type;
Type = int; // The type is integer.
switch (Type) {
case short: // The type is short.
//...
break;
case int: // The type is integer.
//...
break;
case float: // The type is float.
//...
break;
case double: // The type is double.
//...
break;
}I know I can simply declare a bunch of constant numbers, who each represents a variable type. But that would be very non-optimal if there already exists an easier way. Could I use templates to achieve this or the use of preprocessors/macros? Just to be clear, I am looking for a variable that only contains a type and not a value. Any help or tips are most appreciated. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
Boost Any may be of interest: http://www.boost.org/doc/html/any.html[^] Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
-
Off the top of my head, I believe some of Andrei Alexandrescu's work (Loki??) covers situations like this, although I don't have a reference to hand. Also, the VARIANT struct already has some associated defines that could be used as a base for this - eg VT_I4, VT_UI1, VT_BSTR, etc... -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky
There is also Boost Any which may be of interest: http://www.boost.org/doc/html/any.html[^] Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com