rvalue references

C

Cory Nelson

I downloaded GCC 4.3 to try and bring myself up to speed on C++0x
stuff, and have run across a question. Suppose you have a class like
this:

struct foo {
foo(const bar &b) : m_b(b) {}

bar m_b;
};

I want to make m_b construct as efficiently as possible, so I also
give foo an rvalue constructor with move semantics:

struct foo {
foo(const bar &b) : m_b(b) {}
foo(bar &&b) : m_b(std::move(b)) {}

bar m_b;
};

Which is not a big deal for a trivial example, but could mean a lot of
code duplication on something more complex with a number of
parameters.

I don't really care what type of reference b is, I just want to send
it right to m_b's constructor implementing move semantics when
possible. Is there an easy solution to this that I'm not seeing?
 
A

Alf P. Steinbach

* Cory Nelson:
I downloaded GCC 4.3 to try and bring myself up to speed on C++0x
stuff, and have run across a question. Suppose you have a class like
this:

struct foo {
foo(const bar &b) : m_b(b) {}

bar m_b;
};

I want to make m_b construct as efficiently as possible, so I also
give foo an rvalue constructor with move semantics:

struct foo {
foo(const bar &b) : m_b(b) {}
foo(bar &&b) : m_b(std::move(b)) {}

As I understand, all std::move does is cast the argument to bar&&. Which you
already have. So I don't understand the purpose of writing std::move here.

bar m_b;
};

Which is not a big deal for a trivial example, but could mean a lot of
code duplication on something more complex with a number of
parameters.

It seems what you want is perfect forwarding of a single argument of restricted
type, for a function with non-templated arguments.

Well I dunt know. :)

I don't really care what type of reference b is, I just want to send
it right to m_b's constructor implementing move semantics when
possible. Is there an easy solution to this that I'm not seeing?

You could sort of templatize the thing and use boost::disable_if to restrict to
type bar. I think. But would be much more complicated than simply writing the
two constructors.


Cheers,

- Alf


PS: As an interim measure, I suggest posting to [comp.lang.c++.moderated].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top