Boost logo

Boost :

From: e_d_k_at_[hidden]
Date: 2001-05-23 14:01:57


Problems with type_traits

I want to write something like this:

   int x =7;
   double f=98.6;

   pair<iint, double> p1(x,f);
   pair<int,double&> p2(x,f);
   cout << p1.second << endl; // prints 98.6
   cout << p2.second << endl; // prints 98.6
   f = 9.8;
   cout << p1.second << endl; // prints 98.6
   cout << p2.second << endl; // prints 9.8

Of cource this is not posable with std::pair.

So I decided to write an improved pair. I started with the following
code copied form "boost_1_21_2/libs/type_traits/c++_type_traits.htm"

  template <typename T1, typename T2>
  struct pair
  {
     typedef T1 first_type;
     typedef T2 second_type;

     T1 first;
     T2 second;

     pair(boost::add_reference<const T1>::type nfirst,
          boost::add_reference<const T2>::type nsecond)
     :first(nfirst), second(nsecond) { }
  };

but I got errors, starting with :

  Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
  pairtst.cpp:
  Error E2089 C:\cpplibs\edklib\edk_lib/pair.hpp 46: Identifier 'type' cannot have a type qualifier

After much fiddling around I got the following to work.

  template <class T1, class T2>
  struct pair
  {
    typedef T1 first_type;
    typedef boost::add_const<T1>::type const_first_type;
    typedef boost::add_reference<const_first_type>::type const_ref_first_type;
    typedef T2 second_type;
    typedef boost::add_const<T2>::type const_second_type;
    typedef boost::add_reference<const_second_type>::type const_ref_second_type;

    typename T1 first;
    typename T2 second;

    pair (const_ref_first_type a,
          const_ref_second_type b)
          : first(a), second(b) {}

  }

I thought I'd solved my problems, until, I tied to compile this:

     pair<int, double> p3(42,9.989);

I got this error:

  Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
  pairtst.cpp:
  Error E2285 pairtst.cpp 30: Could not find a match for 'pair<int,double>::pair(int,double)' in function main()

Can anyone help me figure out what's wrong and how to fix it?

After I get this working I'd like to write a working make_pair template,
but I can work around that if nessisary.

Thank you,

    -EdK

                                                           


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk