
I'm not sure if this is a stupid question, but is there a runtime equivalent of boost.mpl's identity metafunction? Something that just returns it's single argument, eg., template <typename T> T identity( T t ) { return t; } I think it's trivially simple to write, but I wonder if it already exists in Boost or indeed the STL? (I need this because I need to pass a function object which simply returns an object I already have - maybe I've missed a more obvious method?) Thx, Rob.

AMDG On 11/30/2010 7:06 AM, Robert Jones wrote:
I'm not sure if this is a stupid question, but is there a runtime equivalent of boost.mpl's identity metafunction?
Something that just returns it's single argument, eg.,
template<typename T> T identity( T t ) { return t; }
I think it's trivially simple to write, but I wonder if it already exists in Boost or indeed the STL?
(I need this because I need to pass a function object which simply returns an object I already have - maybe I've missed a more obvious method?)
boost::lambda::_1? In Christ, Steven Watanabe

On Tue, Nov 30, 2010 at 3:20 PM, Steven Watanabe
AMDG
On 11/30/2010 7:06 AM, Robert Jones wrote:
I'm not sure if this is a stupid question, but is there a runtime equivalent of boost.mpl's identity metafunction?
Something that just returns it's single argument, eg.,
template<typename T> T identity( T t ) { return t; }
I think it's trivially simple to write, but I wonder if it already exists in Boost or indeed the STL?
(I need this because I need to pass a function object which simply returns an object I already have - maybe I've missed a more obvious method?)
boost::lambda::_1?
Thx Steven - as usual exactly the right answer to the question. I however have realised I'm asking completely the wrong question. What I in fact want to do is to create on the fly a nullary function object which returns a constant value. Possibly this is what lambda::constant does? Thx, Rob.

AMDG On 12/1/2010 1:00 AM, Robert Jones wrote:
Thx Steven - as usual exactly the right answer to the question.
I however have realised I'm asking completely the wrong question. What I in fact want to do is to create on the fly a nullary function object which returns a constant value.
Possibly this is what lambda::constant does?
Yep. In Christ, Steven Watanabe

On Wednesday 01 December 2010 01:00:42 Robert Jones wrote:
I however have realised I'm asking completely the wrong question. What I in fact want to do is to create on the fly a nullary function object which returns a constant value.
Possibly this is what lambda::constant does?
lambda::constant or phoenix::val. Ravi

On Tue, Nov 30, 2010 at 10:06 AM, Robert Jones
Something that just returns it's single argument, eg.,
template <typename T> T identity( T t ) { return t; } ... (I need this because I need to pass a function object which simply returns an object I already have - maybe I've missed a more obvious method?)
The way you wrote it, wouldn't that actually return a temporary that is a copy of the object you already have? (rather than a const T& or something) I think the unary operator+() has similar semantics by convention, but you'd have to consider the types being used and whether they define it at all.

On Tue, Nov 30, 2010 at 11:48 PM, John B. Turpish
On Tue, Nov 30, 2010 at 10:06 AM, Robert Jones
wrote: Something that just returns it's single argument, eg.,
template <typename T> T identity( T t ) { return t; } ... (I need this because I need to pass a function object which simply returns an object I already have - maybe I've missed a more obvious method?)
The way you wrote it, wouldn't that actually return a temporary that is a copy of the object you already have? (rather than a const T& or something)
It would, (it was off-the-cuff for illustration) but also because my T type is in fact a pointer so copy semantics would be ok. Good point tho'. I have also realised I'm actually asking completely the wrong question in any case, so it's all become a bit irrelevant! Thx, Rob.

On Wed, Dec 1, 2010 at 12:48 AM, John B. Turpish
On Tue, Nov 30, 2010 at 10:06 AM, Robert Jones
wrote: Something that just returns it's single argument, eg.,
template <typename T> T identity( T t ) { return t; } ... (I need this because I need to pass a function object which simply returns an object I already have - maybe I've missed a more obvious method?)
The way you wrote it, wouldn't that actually return a temporary that is a copy of the object you already have? (rather than a const T& or something)
I guess most compilers would apply some form of elision/return value optimization? Cheers, F.
participants (5)
-
Francesco Biscani
-
John B. Turpish
-
Ravi
-
Robert Jones
-
Steven Watanabe