473,729 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining a variable's type

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.

Jul 22 '05 #1
3 1922

"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
Jul 22 '05 #2
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

Jul 22 '05 #3

"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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1521
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?
3
1743
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.
0
1690
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
4
2159
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
5
1810
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 }
9
2787
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...
5
1579
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...
2
2078
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 ?
4
10304
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.
0
8913
marktang
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...
0
9280
jinu1996
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...
1
9200
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,...
0
8144
agi2029
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...
0
6016
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();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
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
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.