Boost logo

Boost Users :

Subject: Re: [Boost-users] Purpose of identity template
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2008-09-04 17:51:51


Annamalai Gurusami <annamalai.gurusami <at> gmail.com> writes:

>
> Hi All,
> http://www.boost.org/doc/libs/1_36_0/libs/multi_index/doc/index.html
> [...]
> I am trying to understand what is the use of the identity<> template
> class. I also noticed the following in the documentation, "The
> predefined identity predicate can be used here as a key extractor;
> identity returns as the key the same object passed as argument." Is
> there any reason why the word object (instead of type) is used in that
> sentence? Also if the identity<> template evaluates to the type given
> as its argument, is there any benefit out of it?

I *think* I know where your confusion stems from: identity<> is
not a purely compile-time entity as some MPL constructs are, but
a conventional type with real run-time functionality (I guess you are
wondering why not use T directly instead of identity<T>, right?):

1. Is there any reason why the word object (instead of type)
  is used in that sentence?

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

2. Also if the identity<> template evaluates to the type given
  as its argument, is there any benefit out of it?

identity<T> does not evaluate to T, as said before it is not
a MPL metafunction but a plain functor much as, for instance,
std::less<> or boost::hash<>.

Clearer now? Don't hesitate to come back if doubts persist.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


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