
Resending... Eric Niebler wrote:
If I were to write an identity that works today *and* tomorrow, I would do it like this:
struct identity { // for C++03 compatibility template<typename Sig> struct result;
template<typename This, typename Arg> struct result<This(Arg)> { typedef Arg type; };
template<typename Arg> Arg &operator()(Arg &arg) const { return arg; }
template<typename Arg> Arg const &operator()(Arg const &arg) const { return arg; }
#if BOOST_HAS_RVALUE_REFS template<typename Arg> Arg operator()(Arg &&arg) const { return std::move(arg); } #endif };
And now in both C++03 and C++0x, I can be sure the following code works:
int i = 0; int const j = 0;
// rvalue result_of<identity(int)>::type k = identity()(1); assert( 1 == k );
// lvalue result_of<identity(int &)>::type l = identity()(i); assert( &l == &i );
// const lvalue result_of<identity(int const &)>::type m = identity()(j); assert( &m == &j );
As Giovanni points out, in C++0x "The values ti are lvalues when the corresponding type Ti is a reference type, and rvalues otherwise."
So Peter, is this wrong?
I'm glad we know how to write the identity function object in C++0x. What about in C++03? -- Eric Niebler Boost Consulting www.boost-consulting.com