Hello all,
Is there a way to determine a variable's type at run-time? The reason
I'm asking is that i have code that looks like this:
template <class T>
Object::Object( int TypeCode, T* data)
{
switch (TypeCode)
case 1:
//Check to see if data is correct type, //according to a lookup table of my own
(1 is //int, 2 for char, etc.)
}
So, I will pass the function a pointer to a variable of a particular
type, and a numeric code representing what that type is. I want to make
sure the code and the data match, and if not, return an error(probably
just print some text, but that's not important). I know I can do this
with manually overloaded constructors, like:
Object::Object( int TypeCode, int* data)
{
if(TypeCode != 1)
{ //Error
}
}
Object::Object( int TypeCode, char* data)
{
if(TypeCode != 2)
{ //Error
}
}
But this can get big and complicated, and I'd rather not.
Is there some alternate way to do this with inheritance, maybe, or is
there a function that tells you, in some form, what type a variable is?
Comparing to make sure two variables are the same type would work, too. 3 1919
"Steve Brown" <Do@not.spam> wrote in message
news:41******** ******@not.spam ... Hello all,
Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this:
template <class T> Object::Object( int TypeCode, T* data) { switch (TypeCode) case 1: //Check to see if data is correct type, //according to a lookup table of my own (1 is //int, 2 for char, etc.) }
Yuck, yuck. But if you really need this
template <class T>
class TypeToInt
{
// value deliberately undefined
};
template <>
class TypeToInt<int>
{
enum { value = 1 };
};
template <>
class TypeToInt<char>
{
enum { value = 2 };
};
template <>
class TypeToInt<doubl e>
{
enum { value = 3 };
};
template <class T>
Object::Object( int TypeCode, T* data)
{
switch (TypeCode)
{
case 1:
if (TypeToInt<T>:: value == 1)
...
} So, I will pass the function a pointer to a variable of a particular type, and a numeric code representing what that type is. I want to make sure the code and the data match, and if not, return an error(probably just print some text, but that's not important). I know I can do this with manually overloaded constructors, like:
Object::Object( int TypeCode, int* data) { if(TypeCode != 1) { //Error } }
Object::Object( int TypeCode, char* data) { if(TypeCode != 2) { //Error } }
But this can get big and complicated, and I'd rather not. Is there some alternate way to do this with inheritance, maybe, or is there a function that tells you, in some form, what type a variable is? Comparing to make sure two variables are the same type would work, too.
That's a little more elegant
template <class T, class U>
class SameType
{
enum { value = 0 };
};
template <class T>
class SameType<T, T>
{
enum { value = 1 };
};
template <class T>
Object::Object( int TypeCode, T* data)
{
switch (TypeCode)
{
case 1:
if (SameType<T, int>::value)
...
}
But for this you need a compiler that supports partial template
specialisation and older compilers often don't.
john
John Harrison wrote: "Steve Brown" <Do@not.spam> wrote in message news:41******** ******@not.spam ...
Hello all,
Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this:
template <class T> Object::Objec t(int TypeCode, T* data) { switch (TypeCode) case 1: //Check to see if data is correct type, //according to a lookup table of my own (1 is //int, 2 for char, etc.) }
Yuck, yuck. But if you really need this
template <class T> class TypeToInt { // value deliberately undefined };
template <> class TypeToInt<int> { enum { value = 1 }; };
template <> class TypeToInt<char> { enum { value = 2 }; };
template <> class TypeToInt<doubl e> { enum { value = 3 }; };
template <class T> Object::Object( int TypeCode, T* data) { switch (TypeCode) { case 1: if (TypeToInt<T>:: value == 1) ... }
So, I will pass the function a pointer to a variable of a particular type, and a numeric code representing what that type is. I want to make sure the code and the data match, and if not, return an error(probably just print some text, but that's not important). I know I can do this with manually overloaded constructors, like:
Object::Objec t(int TypeCode, int* data) { if(TypeCode != 1) { //Error } }
Object::Objec t(int TypeCode, char* data) { if(TypeCode != 2) { //Error } }
But this can get big and complicated, and I'd rather not. Is there some alternate way to do this with inheritance, maybe, or is there a function that tells you, in some form, what type a variable is? Comparing to make sure two variables are the same type would work, too.
That's a little more elegant
template <class T, class U> class SameType { enum { value = 0 }; };
template <class T> class SameType<T, T> { enum { value = 1 }; };
template <class T> Object::Object( int TypeCode, T* data) { switch (TypeCode) { case 1: if (SameType<T, int>::value) ... }
But for this you need a compiler that supports partial template specialisation and older compilers often don't.
john
The last solution looks like what I want, thanks! A couple questions,
though. By saying template<class T, class U>, is it guarunteed that
they will be different types? It seems that that must be true for this
to work? Also, what part of this solution makes use of partial template
specialization? It all seems pretty straightforward to me. Finally,
why use an enum, and not just a bool?
Thanks for your help,
Steve
"Steve Brown" <Do@not.spam> wrote in message
news:41******** ******@not.spam ... John Harrison wrote: "Steve Brown" <Do@not.spam> wrote in message news:41******** ******@not.spam ...
Hello all,
Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this:
template <class T> Object::Obje ct(int TypeCode, T* data) { switch (TypeCode) case 1: //Check to see if data is correct type, //according to a lookup table of my own (1 is //int, 2 for char, etc.) }
Yuck, yuck. But if you really need this
template <class T> class TypeToInt { // value deliberately undefined };
template <> class TypeToInt<int> { enum { value = 1 }; };
template <> class TypeToInt<char> { enum { value = 2 }; };
template <> class TypeToInt<doubl e> { enum { value = 3 }; };
template <class T> Object::Object( int TypeCode, T* data) { switch (TypeCode) { case 1: if (TypeToInt<T>:: value == 1) ... }
So, I will pass the function a pointer to a variable of a particular type, and a numeric code representing what that type is. I want to make sure the code and the data match, and if not, return an error(probably just print some text, but that's not important). I know I can do this with manually overloaded constructors, like:
Object::Obje ct(int TypeCode, int* data) { if(TypeCode != 1) { //Error } }
Object::Obje ct(int TypeCode, char* data) { if(TypeCode != 2) { //Error } }
But this can get big and complicated, and I'd rather not. Is there some alternate way to do this with inheritance, maybe, or is there a function that tells you, in some form, what type a variable is? Comparing to make sure two variables are the same type would work, too.
That's a little more elegant
template <class T, class U> class SameType { enum { value = 0 }; };
template <class T> class SameType<T, T> { enum { value = 1 }; };
template <class T> Object::Object( int TypeCode, T* data) { switch (TypeCode) { case 1: if (SameType<T, int>::value) ... }
But for this you need a compiler that supports partial template specialisation and older compilers often don't.
john
The last solution looks like what I want, thanks! A couple questions, though. By saying template<class T, class U>, is it guarunteed that they will be different types? It seems that that must be true for this to work? Also, what part of this solution makes use of partial template specialization? It all seems pretty straightforward to me. Finally, why use an enum, and not just a bool?
Thanks for your help, Steve
When you write 'SameType<X, Y>::value' the compiler has to choose between
the two versions of SameType. The first is the unspecialised version where X
and Y can be any type, the second is the partially specialised version where
X and Y must be the same type. The rules for which is chosen are complex,
but essentially it boils down to the compiler chooses the most specialised
version which can possibly apply. So, to answer your first question, T and U
could be the same type but for this particular code whenever T and U would
be the same type then the second version is chosen instead.
This is the part that uses partial specialisation.
template <class T>
class SameType<T, T>
{
enum { value = 1 };
};
Its a template specialisation (because it specialises the previous SameType
template) but its not a full specialisation because there is still one
template parameter, namely T.
Incidentally there are ways to solve this without using partial template
specialisation so if your compiler chokes on the above, then I could show
you another method. VC++ 6 is the compiler that notoriously doesn't support
partial template specialisation.
Using enums is just personal preference. This is perfectly good and legal
C++
template <class T, class U>
class SameType
{
static const bool value = false;
};
But some compilers have issues with that kind of code.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mike Kamermans |
last post by:
Is there a way to determine what a node() find returns? For instance, if
I have a snip of XSLT that looks like:
<xsl:for-each select="node()">
...
</xsl:for-each>
Is there a way to, for each node found, generate what kind of node it
found, like <some_element> or <#PCDATA> or something that indicates the
type of element it found?
|
by: Trevor M. Lango |
last post by:
The number of arguments passed to the following functions can be
determined - but how is this coded?
main (int argc, char *argv)
printf ( string format )
I would like to be able to write arbitrary functions that know how many
arguments were passed to them.
|
by: CTDev Team |
last post by:
Hi,
We are using Exchange Server 5.5, and have applications written in VB6 and
C# that read and process emails.
We are experiencing intermittent errors similar to
C# Application
System.Runtime.InteropServices.COMException (0x80004005): The client
|
by: petermichaux |
last post by:
Hi,
I'm hoping for a reason I'm wrong or an alternate solution...
I'd like to be able to dynamically include some javascript files. This
is like scriptaculous.js library but their solution is broken in
Firefox 1.5.0.1 on OS X. What happens with the Scriptaculous library is
this
In the html document the author only has to include one line
|
by: Doug Stiers |
last post by:
C# newbie question:
I'm trying to determine the type of a datareader value. Why cant I do it
like this?
if (rdr.GetValue(0).GetType() is Guid || rdr.GetValue(0).GetType() is
Int32)
{
// do something
}
| |
by: Schraalhans Keukenmeester |
last post by:
I have some C functions (with variable length argument lists) that use
void pointers as arguments.
Is there a way to determine at runtime what type of parameter is
actually passed on to the function? PHP and my oldskool turbopascal
provide a typeof() function, but my C compiler (gcc 3.4.1) does not seem
to provide this function. Perhaps someone crafted a library with some
smart code able to inspect the variable passed to a certain...
|
by: Jeremy |
last post by:
Does anyone have a clever algorithm for generating an outline of the
current document from (client-side) javascript using DOM methods?
For example, let's say I predictably have a document structured
hierarchically with <h1>...<h6tags. I want to generate an outline of
the document wherein I have nested lists of the contents of the headers.
Take for example the following snippet of a fictional legal document:
------------
<h1>Main...
|
by: pob |
last post by:
I was all happy because thru some tutelage of others I created this
array to find the newest file in a directory. However, I then
realized its great that I now know the latest file, but I need to know
the name of the file. Unfortunately, I am only passing the date into
the array.
Do I need to create a m-d array ? If so how do I create a 2-d array
where I do not know how many rows I will be looping thru ?
|
by: mouseit |
last post by:
If I have a variable of some sort, say x, how can I find out what
variable type javascript thinks it is? For example, if I've declared x
as 5 earlier, how do I know now whether it's a string or a number,
isNaN returns false, regardless of whether I declared it as "5" or 5.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |