Boost logo

Boost Users :

From: Chris Goller (cgoller_at_[hidden])
Date: 2005-02-15 12:42:27


I just finished the first exercise in the C++ meta programming book.
Then I checked what had been done before on this mailing list.

Here is my solution:

using namespace boost;

template <bool use_ref, typename T>
struct add_const_ref_impl
{
    typedef T const & type;
};

template <typename T>
struct add_const_ref_impl<true, T>
{
    typedef T type;
};

template<class T>
struct add_const_ref
{
    typedef typename add_const_ref_impl<is_reference<T>::value, T>::type
type;
};

 From Bruce Trask comes:

using namespace boost;

template<typename T>
struct add_const_ref
{
  typedef typename add_reference<typename add_const<T>::type>::type type;
};

His solution is SO much better then mine.

However, when I first saw it I thought it must be wrong because I
assumed that add_const on a reference would would return a const reference.
In other words, the code below would be false:

bool a = is_same<add_const<int &>::type, int &>::value;

However! bool "a" is true. So my question is why is "a" true? Why
isn't add_const<int &>::type == int const &?

Thanks,

Chris


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net