problems of promotion traits

I have some problems about promotion traits
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
template<typename T1, typename T2>
class Promotion;

template<bool C, typename Ta, typename Tb>
class IfThenElse;

template<typename Ta, typename Tb>
class IfThenElse<true, Ta, Tb>
{
  public :
    typedef Ta ResultT;
};

template<typename Ta, typename Tb>
class IfThenElse<false, Ta, Tb>
{
  public :
    typedef Tb ResultT;
};

template<typename T1, typename T2>
class Promotion
{
  public :
    typedef typename IfThenElse< (sizeof(T1) > sizeof(T2)),
                                  T1,
                                  typename IfThenElse<(sizeof(T1) < sizeof(T2)),
                                  T2,
                                  void>::ResultT>::ResultT ResultT;
};


Why did they make the code of promotion looks like this
1
2
3
typename IfThenElse<(sizeof(T1) < sizeof(T2)),
                                  T2,
                                  void>::ResultT

but not
1
2
3
4
5
6
template<typename T1, typename T2>
class Promotion
{
  public :
    typedef typename IfThenElse< (sizeof(T1) > sizeof(T2)), T1, T2>::ResultT ResultT;
};


Thanks a lot
Last edited on

Both seem to compile and output the same results on Ubuntu 64 using gcc:

with #define ORIG
8
16
16
x
987.65


without #define ORIG
8
16
16
x
987.65


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>

using namespace std;

template<typename T1, typename T2>
class Promotion;

template<bool C, typename Ta, typename Tb>
class IfThenElse;

template<typename Ta, typename Tb>
class IfThenElse<true, Ta, Tb>
{
  public :
    typedef Ta ResultT;
};

template<typename Ta, typename Tb>
class IfThenElse<false, Ta, Tb>
{
  public :
    typedef Tb ResultT;
};

// #define ORIG

#ifdef ORIG
template<typename T1, typename T2>
class Promotion
{
  public :
    typedef typename IfThenElse< (sizeof(T1) > sizeof(T2)),
                                  T1,
                                  typename IfThenElse<(sizeof(T1) < sizeof(T2)),
                                  T2,
                                  void>::ResultT>::ResultT ResultT;
};
#else
template<typename T1, typename T2>
class Promotion
{
  public :
	    typedef typename IfThenElse< (sizeof(T1) > sizeof(T2)), T1, T2>::ResultT ResultT;
};
#endif

struct Foo {
	Foo() : d(987.65) { }
	double d;
};

struct Bar : public Foo {
	Bar() : c('x') { }
	char c;
};

main()
{
	Promotion< Foo, Bar >::ResultT x;

	cout << sizeof( Foo ) << endl;
	cout << sizeof( Bar ) << endl;
	cout << sizeof( x    ) << endl;
	cout << sizeof( x.c  ) << endl;
	cout << sizeof( x.d  ) << endl;
}
Topic archived. No new replies allowed.