Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-02-15 13:47:22


Chris Goller <cgoller_at_[hidden]> writes:

> His solution is SO much better then mine.

You can do even better (hint: you don't need to use add_const).

> 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 &?

It's not a very interesting answer: that's just the way the C++ type
system works.

The rule is, essentially, that

  add_const<T>::type

is equivalent to

  T const.

When you do that with int&, you get

  int& const

Think of that as being like

  int* const

The int isn't const, here; the pointer is. But unlike pointers,
references can't be made to refer to new things. So

  int& const

and

  int&

are really the same type. Both are different from

  int const&

where the int is immutable through that reference.

HTH,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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