Variable detection help

Hello everybody...Currently I am making a simple program which only contains an input, and output code, and finally an union. The goal is how to detect type of a variable correctly. Here is the code :

1
2
3
4
5
6
7
8
9
10
union SuperVariable
{
int i;
float f;
} var;
//////
var.i = 85; 
bool IsDouble = Check(); //Example : It gives "false" (integer)
var.f = 21.81f;
IsDouble = Check(); //Example : It gives "true" (decimal number) 

Is there any way which can do this? Help me...
(Any help would be greatly appreciated) (^_^)
Last edited on
if you want to see if its double or not you could use sizeof

double is 8byte
all the other datatypes are smaller, as long as you don't use "long"

edit: i thinktypeidandtype_info is what you are looking for
Last edited on
Not possible. You will have to keep track of the type yourself.
Is there any way which can do this?
Not with a union (@Darkmaster: no, sizeof doesn't work since the union will have a certain size which will not change)

You may want to use Variant instead:

http://www.boost.org/doc/libs/1_52_0/doc/html/variant.html

or make your own struct that keeps the type
@coder777: I don't see the problem with this.
I just tried this and it works
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
union SuperVariable
{
	char c;
	int i;
	float f;
	double d;
}var;

int main()
{
	var.c = 1;
	var.i = 1;
	var.f = 1;
	var.d = 1;

	int c = sizeof(var.c);
	int i = sizeof(var.i);
	int f = sizeof(var.f);
	int d = sizeof(var.d);
}


c=1
i=4
f=4
d=8
@Darkmaster
That will not help you knowing what type is actually being "used". If you do sizeof(var) it will always give you the same value (probably 8).
Thanks guys!

I have an another question :
1
2
3
4
5
union SuperVariable
{
int i;
float f;
} var;

sizeof(int) == sizeof(float) (= 4)


1
2
3
4
5
union SuperVariable
{
long l;
double d;
} var;


sizeof(long) == sizeof(double) (= 8)

Sorry, my compiler doesn't support 32-bit long type variable...

Here's how... (draft)
1
2
3
4
5
6
7
8
9
/Invalid value detector

var.d = 35.56;
//var.d : 35.56 (Valid) -> Take
//var.l : ????? (Skip)

var.l = 100
//var.l : 100 (Valid) -> Take
//var.d : ????? (Skip) 

But...Can I solve the problem by extracting a specific bit of the generic variable?
Last edited on
> Can I solve the problem by extracting a specific bit of the generic variable?

You can solve the problem by looking at the discriminant of a union-like class.

Something homegrown would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

struct number
{
    number( int ii = 0 ) : t(INT), i(ii) {}
    number( double dd ) : t(DOUBLE), d(dd) {}

    bool is_int() const { return t == INT ; } ;
    bool is_double() const { return t == DOUBLE ; } ;

    explicit operator int() const { return is_int() ? i : int(d) ; }
    explicit operator double() const { return is_double() ? d : i ; }

    private:
        enum type { INT, DOUBLE } ;
        type t ;
        union { int i ; double d ; } ;
};

inline std::ostream& operator<< ( std::ostream& stm, number n )
{
    if( n.is_int() ) return stm << int(n) ;
    else return stm << double(n) ;
}

int main()
{
     number n = 12 ;
     std::cout << std::boolalpha << n.is_int() << ' ' << n << '\n' ;
     n = 34.567 ;
     std::cout << std::boolalpha << n.is_double() << ' ' << n << '\n' ;


In practice, use either boost::any or boost::variant
Topic archived. No new replies allowed.