This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Is this a compiler fault, or is my code buggy? (I suspect thelatter).


On compiling, the error messages suggest that var1, 2, and 3 are all
getting the same type t2, while in fact, the variable var1 should get
type t3. What's wrong here?




-- 
	-Dhruv Matani.
http://www.geocities.com/dhruvbird/


#include <iostream>



template <class T1, class T2>
struct same_type {
  static const bool result = false;
};

template <class T1>
struct same_type<T1, T1> {
  static const bool result = true;
};



template <bool Condition, class FT, class ST>
struct IF_ {
  typedef ST type;
};

template <class FT, class ST>
struct IF_<true, FT, ST> {
  typedef FT type;
};

template <class A, class B>
struct smaller_2 {
  typedef typename IF_< (sizeof(A) < sizeof(B)), A, B>::type type;
};


template <class A, class B>
struct larger_size_2 {
  typedef typename IF_< (sizeof(A) > sizeof(B)), A, B>::type type;
};

template <class A, class B, class C>
struct larger_size_3 {
  typedef typename 
  IF_< (sizeof (typename larger_size_2<A, B>::type) > sizeof (C)), 
    typename larger_size_2<A, B>::type, C>::type type;
};

template <class A, class B, class C>
struct second_largest_3 {
  typedef typename larger_size_3 <A, B, C>::type Exclude;
  typedef typename
  IF_ <same_type <Exclude, A>::result, B, A>::type first_type;
  typedef typename
  IF_ <same_type <Exclude, A>::result, C, 
       typename smaller_2<B, A>::type>::type second_type;
  typedef typename larger_size_2 <first_type, second_type>::type type;
};

template <class A, class B, class C>
struct smallest_3 {
  typedef typename 
  smaller_2 <typename second_largest_3 <A, B, C>::second_type, 
	     typename second_largest_3 <A, B, C>::first_type>::type type;
};


template <class A, class B, class C>
struct foo {
  typename larger_size_3 <A, B, C>::type var1;
  typename second_largest_3 <A, B, C>::type var2;
  typename smallest_3 <A, B, C>::type var3;
  //  foo() { }
  foo () {
    var1 = "dhruv";
    var2 = "dd";
    var3 = "445";
  }

};








int main ()
{
  typedef double t1;
  typedef double t2;
  typedef double t3;
  foo <t1, t2, t3> ofoo;
//   if (sizeof(t1) > sizeof (t2))
//     std::cout<<"True";
//   else {
//     std::cout<<"False";
//     if (sizeof (t2) > sizeof (t3))
//       std::cout<<"Yes";
//     else
//       std::cout<<"Nop ";
//   }
}



















Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]