|
Boost Users : |
Subject: Re: [Boost-users] Boost.Move template-function parameter
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-01-12 18:20:27
On Thu, Jan 12, 2012 at 2:27 AM, Luke J. West <ljw_at_[hidden]> wrote:
> Hi. I'm trying to implement a template function that 'moves' it's
> parameter. Here's a simple moveable class...
>
>
> struct my_class {
> my_class() {}
> my_class(BOOST_RV_REF(my_**class)) {}
>
> my_class& operator=(BOOST_COPY_ASSIGN_**REF(my_class) c) {return *this;}
>
> BOOST_COPYABLE_AND_MOVABLE(my_**class);
> };
>
>
> And here is the function...
>
>
> template<typename T > void steal(BOOST_COPY_ASSIGN_REF(T) c) {}
>
Unrelated: Why use BOOST_COPY_ASSIGN_REF( T ) as the function parameter?
Pretty sure this macro is only to be used within a operator=
declaration/definition. Should probably use BOOST_RV_REF( T ).
> so it works fine when I do this...
>
> int main() {
> my_class c;
> steal<my_class >(c);
> }
>
> but not when I do this...
>
> int main() {
> int c;
> steal<int >(c);
> }
>
>
> It's as if 'int' is not a movable type which of course it trivially is. I
> read that Boost.Container classes are 'move aware' and I assume
> boost::vector<int > works, so what am I doing wrong - how to write my
> 'steal' function capable of moving it's argument when it's moveable class,
> but not breaking for primitives?
>
> Thanks,
>
> Luke (hopeful newbie!)
>
Hmmm...right...I think the compiler doesn't like instantiating
boost::rv<int>. AFAIK a trac ticket had just been recently filed to this
effect.
Note that in C++11, lvalues are not implicitly convertible to rvalue
references. So if you actually wanted steal to take an (emulated) rvalue
reference, you should still call it with an enclosed boost::move:
steal(boost::move(c))
and if you know that steal will always be accepting a moveable object, you
can declare it as
template< class T > void steal(BOOST_RV_REF( T ));
Can you give a better idea of what you're trying to achieve?
- Jeff
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