
This handy little item, lvalue_cast, would make a useful addition to boost. I didn't write it, and I don't recall who did. template<class T> inline T& lvalue_cast (const T& rvalue) { return const_cast<T&> (rvalue); } Example usage: #include <vector> #include <boost/range.hpp> template<class T> inline T& lvalue_cast (const T& rvalue) { return const_cast<T&> (rvalue); } template<typename in_cont_t> int F (in_cont_t & in) { return 2; } int main() { std::vector<int> v (4); F (lvalue_cast (boost::make_iterator_range (v.begin(), v.end()))); << OK F (boost::make_iterator_range (v.begin(), v.end())); << error! } -- Please AVOID sending me WORD, EXCEL or POWERPOINT attachments. See http://www.fsf.org/philosophy/no-word-attachments.html

Neal D. Becker wrote:
This handy little item, lvalue_cast, would make a useful addition to boost. I didn't write it, and I don't recall who did.
template<class T> inline T& lvalue_cast (const T& rvalue) { return const_cast<T&> (rvalue); }
I wrote something like this, that has the upshot of being safe. ;) http://article.gmane.org/gmane.comp.lib.boost.devel/101531 -- Daniel Wallin

Daniel Wallin wrote:
Neal D. Becker wrote:
This handy little item, lvalue_cast, would make a useful addition to boost. I didn't write it, and I don't recall who did.
template<class T> inline T& lvalue_cast (const T& rvalue) { return const_cast<T&> (rvalue); }
I wrote something like this, that has the upshot of being safe. ;)
The trick would be to get it to return a const lvalue when you pass a const rvalue ;-) -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

David Abrahams wrote:
Daniel Wallin wrote:
I wrote something like this, that has the upshot of being safe. ;)
The trick would be to get it to return a const lvalue when you pass a const rvalue ;-)
Ah yes, and to do that you need to help with the overload resolution. Like you did in mentioned thread, right? -- Daniel Wallin

"Neal D. Becker" <nbecker@hns.com> writes:
This handy little item, lvalue_cast, would make a useful addition to boost. I didn't write it, and I don't recall who did.
The problem with that is that casting away the const on a const rvalue is sometimes unsafe. See the original move semantics proposal for details. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

David Abrahams wrote:
"Neal D. Becker" <nbecker@hns.com> writes:
This handy little item, lvalue_cast, would make a useful addition to boost. I didn't write it, and I don't recall who did.
The problem with that is that casting away the const on a const rvalue is sometimes unsafe. See the original move semantics proposal for details.
Not sure what you're saying here. I agree it can sometimes be unsafe. That's why you shouldn't use it - except when you really want to. Isn't that consistent with the move semantics proposal? The main use I had in mind, is when you want: void F (T1 in, T2 &out) { write to out } F (something, make_temp()) Where you don't really care that F is going to write to the temporary, it's really not used. Other approaches are possible here, e.g., boost::optional - but this was intended for the case where F was already written to a certain interface.

Neal Becker <ndbecker2@verizon.net> writes:
David Abrahams wrote:
"Neal D. Becker" <nbecker@hns.com> writes:
This handy little item, lvalue_cast, would make a useful addition to boost. I didn't write it, and I don't recall who did.
The problem with that is that casting away the const on a const rvalue is sometimes unsafe. See the original move semantics proposal for details.
Not sure what you're saying here. I agree it can sometimes be unsafe. That's why you shouldn't use it - except when you really want to. Isn't that consistent with the move semantics proposal?
No. Nothing in the move semantics proposal suggests that the constness of a const rvalue should be removed. It only changes const rvalues into const lvalues. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

From: "Neal D. Becker" <nbecker@hns.com>
This handy little item, lvalue_cast, would make a useful addition to boost. I didn't write it, and I don't recall who did.
template<class T> inline T& lvalue_cast (const T& rvalue) { return const_cast<T&> (rvalue); }
That's just a different spelling of const_cast. To make it useful, you need to make it more generic: template <typename T> inline T & lvalue_cast(T & value_i) { return value_i; } That, of course, permits you to silently cast away the const-ness of const lvalues. Thus, Daniel Wallin's version, at http://article.gmane.org/gmane.comp.lib.boost.devel/101531, is much better. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

Rob Stewart <stewart@sig.com> writes:
From: "Neal D. Becker" <nbecker@hns.com>
This handy little item, lvalue_cast, would make a useful addition to boost. I didn't write it, and I don't recall who did.
template<class T> inline T& lvalue_cast (const T& rvalue) { return const_cast<T&> (rvalue); }
That's just a different spelling of const_cast. To make it useful, you need to make it more generic:
template <typename T> inline T & lvalue_cast(T & value_i) { return value_i; }
That, of course, permits you to silently cast away the const-ness of const lvalues.
That's news to me. AFAICT it's totally safe, but it doesn't work with rvalues, which sort of undermines the whole thing. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

From: David Abrahams <dave@boost-consulting.com>
Rob Stewart <stewart@sig.com> writes:
From: "Neal D. Becker" <nbecker@hns.com>
template<class T> inline T& lvalue_cast (const T& rvalue) { return const_cast<T&> (rvalue); }
That's just a different spelling of const_cast. To make it useful, you need to make it more generic:
template <typename T> inline T & lvalue_cast(T & value_i) { return value_i; }
That, of course, permits you to silently cast away the const-ness of const lvalues.
That's news to me. AFAICT it's totally safe, but it doesn't work with rvalues, which sort of undermines the whole thing.
I didn't make it clear, I see now, but I meant adding the overload so that both rvalues and lvalues could be passed through lvalue_cast to get an lvalue. -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;
participants (5)
-
Daniel Wallin
-
David Abrahams
-
Neal Becker
-
Neal D. Becker
-
Rob Stewart