rvalue references

Discussion in 'C++' started by Cory Nelson, Mar 19, 2008.

  1. Cory Nelson

    Cory Nelson Guest

    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?
    Cory Nelson, Mar 19, 2008
    #1
    1. Advertising

  2. perhaps comp.std.c++ ?

    Cory Nelson wrote:
    > 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?
    Gianni Mariani, Mar 20, 2008
    #2
    1. Advertising

  3. Cory Nelson

    Guest

    On Mar 20, 4:19 am, Gianni Mariani <> wrote:
    > perhaps comp.std.c++ ?
    >
    >


    That newsgroup is turning into a ghost town. At this time I
    don't have much to add to the discussion here except that it reminds
    me a little of a thread on comp.std.c++, before it turned into a ghost
    town, titled "for each member of a sturcture/class ?".
    http://preview.tinyurl.com/2yjh7y

    Brian Wood
    Ebenezer Enterprises
    www.webebenezer.net
    , Mar 20, 2008
    #3
  4. * 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].

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 20, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kavya
    Replies:
    9
    Views:
    494
    Dik T. Winter
    Oct 28, 2006
  2. Chris Fairles
    Replies:
    1
    Views:
    363
    Howard Hinnant
    Sep 4, 2007
  3. Paul Brettschneider

    rvalue references and default constructors

    Paul Brettschneider, Mar 26, 2008, in forum: C++
    Replies:
    3
    Views:
    766
    Frank Bergemann
    Apr 16, 2008
  4. Juha Nieminen
    Replies:
    13
    Views:
    599
    Edek Pienkowski
    Aug 29, 2012
  5. K. Frank
    Replies:
    4
    Views:
    171
Loading...

Share This Page