Boost logo

Boost Users :

Subject: Re: [Boost-users] Purpose of identity template
From: Annamalai Gurusami (annamalai.gurusami_at_[hidden])
Date: 2008-09-04 23:17:40


On Thu, Sep 4, 2008 at 4:51 PM, Joaquin M Lopez Munoz <joaquin_at_[hidden]> wrote:
> Because identity<T> operates at run time as a functor taking objects
> of type T:
>
> identity<std::string> i;
> std::string str="hello";
> std::cout<<i(str)<<std::endl; // prints hello

Thank you very much for the response. Now I understand that
identity<> is not an MPL construct. I was reading through the
identity.hpp file and I think I understand it now. It can be used to
convert chained pointers and smart pointers to references. For
example,

int ****i;
identity<int> get_ref;
get_ref(i); // This will return me a reference to the integer

Let me know if I got that right. But since the template arguments are
type names is there any reason why we need to use identity in template
arguments? For example, in the code snippet given in the
documentation, (my question embedded in the code fragment below as
comments),

typedef multi_index_container<
  employee,
  indexed_by<
    ordered_unique<identity<employee> >,
// Why is identity required here?
// Why is ordered_unique<employee> not sufficient?
    ordered_non_unique<member<employee,std::string,&employee::name> >,
    ordered_unique<member<employee,int,&employee::ssnumber> >
>
> employee_set;

Since the std::set doesn't require anything other than the type name
of the elements it would hold, why does
boost::multi_index::ordered_unique need this identity<>? What are
the additional benefits of this added complexity?

Rgds,
anna

-- 
Abusive Language on Internet
http://missingrainbow.blogspot.com/2008/08/abusive-language-on-internet.html

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