
I use the following function on a regular basis: template < class P_T > P_T const &same (P_T const &p) { return p; } I use it for two purposes: 1. to pass an identity function to an API that expects a unary function, 2. to help the compiler identify the right chain of type conversions. The standard uses static_cast for (2), which could be used but it disables some compile-time verification so I would rather not use static_cast when not absolutely necessary. I have been unable to find a corresponding feature in Boost (although the value factory comes close). Do you think adding such a function to Boost utilities would be worthwhile? Chris

W dniu 1 października 2011 14:57 użytkownik Krzysztof Żelechowski < giecrilj@stegny.2a.pl> napisał:
I use the following function on a regular basis:
template < class P_T > P_T const &same (P_T const &p) { return p; }
I use it for two purposes:
1. to pass an identity function to an API that expects a unary function, 2. to help the compiler identify the right chain of type conversions.
The standard uses static_cast for (2), which could be used but it disables some compile-time verification so I would rather not use static_cast when not absolutely necessary.
I have been unable to find a corresponding feature in Boost (although the value factory comes close). Do you think adding such a function to Boost utilities would be worthwhile?
Chris
I'm looking for somthing like this too. Kris

2011/10/1 Krzysztof Żelechowski
I use the following function on a regular basis:
template < class P_T > P_T const &same (P_T const &p) { return p; }
I use it for two purposes:
1. to pass an identity function to an API that expects a unary function, 2. to help the compiler identify the right chain of type conversions.
The standard uses static_cast for (2), which could be used but it disables some compile-time verification so I would rather not use static_cast when not absolutely necessary.
I have been unable to find a corresponding feature in Boost (although the value factory comes close). Do you think adding such a function to Boost utilities would be worthwhile?
IMHO such one-liners are better kept not only out of Boost but out of any documented headers and/or namespaces. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode

On 10/1/11 8:57 AM, Krzysztof Żelechowski wrote:
I use the following function on a regular basis:
template< class P_T> P_T const&same (P_T const&p) { return p; }
I use it for two purposes:
1. to pass an identity function to an API that expects a unary function, 2. to help the compiler identify the right chain of type conversions.
The standard uses static_cast for (2), which could be used but it disables some compile-time verification so I would rather not use static_cast when not absolutely necessary.
I have been unable to find a corresponding feature in Boost (although the value factory comes close). Do you think adding such a function to Boost utilities would be worthwhile?
Chris
That sounds basically like implicit_cast<T>(), which is in implicit_cast.hpp

AMDG On 10/01/2011 05:57 AM, Krzysztof Żelechowski wrote:
I use the following function on a regular basis:
template < class P_T > P_T const &same (P_T const &p) { return p; }
I use it for two purposes:
1. to pass an identity function to an API that expects a unary function,
Use boost::lambda::_1. In Christ, Steven Watanabe
participants (5)
-
Emil Dotchevski
-
Krzysztof Czainski
-
Krzysztof Żelechowski
-
Richard Damon
-
Steven Watanabe