std::move in methode

Discussion in 'C++' started by Chris Forone, Mar 28, 2013.

  1. Chris Forone

    Chris Forone Guest

    does the std::move() assignement make sense here or is it used only in
    ctors?

    void Matrix::Ortho(GLfloat right, GLfloat top, GLfloat near, GLfloat far)
    {
    std::array<GLfloat, 16> temp =
    {
    1.0f / right, 0.0f, 0.0f, 0.0f,
    0.0f, 1.0f / top, 0.0f, 0.0f,
    0.0f, 0.0f, -2.0f / (far - near), 0.0f,
    0.0f, 0.0f, -(far + near) / (far - near), 1.0f
    };

    matrix = std::move(temp); // matrix = private class member
    }

    thanks

    cheers, chris
    Chris Forone, Mar 28, 2013
    #1
    1. Advertising

  2. Chris Forone

    Öö Tiib Guest

    On Thursday, 28 March 2013 11:12:50 UTC+2, Chris Forone wrote:
    > does the std::move() assignement make sense here or is it used only in
    > ctors?


    It may make sense when the elements of array are movable.
    You have std::array of floats so those will be copied, there's no
    benefit of moving floats.

    > void Matrix::Ortho(GLfloat right, GLfloat top, GLfloat near, GLfloat far)
    > {
    > std::array<GLfloat, 16> temp =
    > {
    > 1.0f / right, 0.0f, 0.0f, 0.0f,
    > 0.0f, 1.0f / top, 0.0f, 0.0f,
    > 0.0f, 0.0f, -2.0f / (far - near), 0.0f,
    > 0.0f, 0.0f, -(far + near) / (far - near), 1.0f
    > };
    >
    > matrix = std::move(temp); // matrix = private class member


    Use simply 'matrix = temp;' here.

    > }
    Öö Tiib, Mar 28, 2013
    #2
    1. Advertising

  3. Chris Forone

    Chris Forone Guest

    Am 28.03.2013 10:41, schrieb Öö Tiib:
    > On Thursday, 28 March 2013 11:12:50 UTC+2, Chris Forone wrote:
    >> does the std::move() assignement make sense here or is it used only in
    >> ctors?

    >
    > It may make sense when the elements of array are movable.
    > You have std::array of floats so those will be copied, there's no
    > benefit of moving floats.
    >
    >> void Matrix::Ortho(GLfloat right, GLfloat top, GLfloat near, GLfloat far)
    >> {
    >> std::array<GLfloat, 16> temp =
    >> {
    >> 1.0f / right, 0.0f, 0.0f, 0.0f,
    >> 0.0f, 1.0f / top, 0.0f, 0.0f,
    >> 0.0f, 0.0f, -2.0f / (far - near), 0.0f,
    >> 0.0f, 0.0f, -(far + near) / (far - near), 1.0f
    >> };
    >>
    >> matrix = std::move(temp); // matrix = private class member

    >
    > Use simply 'matrix = temp;' here.
    >
    >> }

    >

    is there a better container for this? does std::vector can move its content?
    Chris Forone, Mar 28, 2013
    #3
  4. Chris Forone

    SG Guest

    On Mar 28, 11:20 am, Chris Forone wrote:
    > Am 28.03.2013 10:41, schrieb Öö Tiib:
    > > On Thursday, 28 March 2013 11:12:50 UTC+2, Chris Forone  wrote:
    > >> does the std::move() assignement make sense here or is it used only in
    > >> ctors?

    >
    > > It may make sense when the elements of array are movable.
    > > You have std::array of floats so those will be copied, there's no
    > > benefit of moving floats.

    >
    > >> void Matrix::Ortho(GLfloat right, GLfloat top, GLfloat near, GLfloat far)
    > >> {
    > >>        std::array<GLfloat, 16> temp =
    > >>        {
    > >>                1.0f / right, 0.0f, 0.0f, 0.0f,
    > >>                0.0f, 1.0f / top, 0.0f, 0.0f,
    > >>                0.0f, 0.0f, -2.0f / (far - near), 0.0f,
    > >>                0.0f, 0.0f, -(far + near) / (far - near), 1.0f
    > >>        };
    > >>        matrix = std::move(temp); // matrix = private classmember

    >
    > > Use simply 'matrix = temp;' here.

    >
    > is there a better container for this? does std::vector can move its content?


    Yes, but you would just trade the costs of copying elements for the
    costs of dynamic memory allocation/deallocation. I don't think that
    this will improve the performance because copying 16 floats is rather
    cheap.

    Why don't you modify this->matrix directly?

    I have tried a couple of linear algebra libraries and kind of settled
    on Eigen by now. It allows statically-fixed as well as dynamic matrix/
    vector sizes. In case of statically-fixed sizes there is no heap
    allocation involved. Eigen also supports a nice syntax of directly
    overwriting a matrix' coefficients:

    typedef
    Eigen::Matrix<float,4,4,Eigen::ColMajor>
    mat4f_colmajor; // just like OpenGL wants it

    :::

    mat4f_colmajor mat; // somewhere

    :::

    mat << 1.0f/right, 0.0f, 0.0f, 0.0f,
    0.0f, 1.0f/top, 0.0f, 0.0f,
    0.0f, 0.0f, -2.0f/(far-near), 0.0f,
    0.0f, 0.0f, -(far+near)/(far-near), 1.0f;

    :::

    glLoadMatrixf(mat.data());


    Cheers!
    SG
    SG, Mar 28, 2013
    #4
  5. Chris Forone

    Öö Tiib Guest

    On Thursday, 28 March 2013 12:20:01 UTC+2, Chris Forone wrote:
    > Am 28.03.2013 10:41, schrieb Öö Tiib:
    > > On Thursday, 28 March 2013 11:12:50 UTC+2, Chris Forone wrote:
    > >> does the std::move() assignement make sense here or is it used only in
    > >> ctors?

    > >
    > > It may make sense when the elements of array are movable.
    > > You have std::array of floats so those will be copied, there's no
    > > benefit of moving floats.
    > >
    > >> void Matrix::Ortho(GLfloat right, GLfloat top, GLfloat near, GLfloat far)
    > >> {
    > >> std::array<GLfloat, 16> temp =
    > >> {
    > >> 1.0f / right, 0.0f, 0.0f, 0.0f,
    > >> 0.0f, 1.0f / top, 0.0f, 0.0f,
    > >> 0.0f, 0.0f, -2.0f / (far - near), 0.0f,
    > >> 0.0f, 0.0f, -(far + near) / (far - near), 1.0f
    > >> };
    > >>
    > >> matrix = std::move(temp); // matrix = private class member

    > >
    > > Use simply 'matrix = temp;' here.
    > >
    > >> }

    > >

    >
    > is there a better container for this? does std::vector can move its content?



    'std::valarray<float>' is better designed for doing mathematical
    operations than vector or array but I would go farther and use some
    linear algebra library instead.
    Öö Tiib, Mar 28, 2013
    #5
  6. Chris Forone

    Chris Forone Guest

    Am 28.03.2013 12:36, schrieb Öö Tiib:
    > On Thursday, 28 March 2013 12:20:01 UTC+2, Chris Forone wrote:
    >> Am 28.03.2013 10:41, schrieb Öö Tiib:
    >>> On Thursday, 28 March 2013 11:12:50 UTC+2, Chris Forone wrote:
    >>>> does the std::move() assignement make sense here or is it used only in
    >>>> ctors?
    >>>
    >>> It may make sense when the elements of array are movable.
    >>> You have std::array of floats so those will be copied, there's no
    >>> benefit of moving floats.
    >>>
    >>>> void Matrix::Ortho(GLfloat right, GLfloat top, GLfloat near, GLfloat far)
    >>>> {
    >>>> std::array<GLfloat, 16> temp =
    >>>> {
    >>>> 1.0f / right, 0.0f, 0.0f, 0.0f,
    >>>> 0.0f, 1.0f / top, 0.0f, 0.0f,
    >>>> 0.0f, 0.0f, -2.0f / (far - near), 0.0f,
    >>>> 0.0f, 0.0f, -(far + near) / (far - near), 1.0f
    >>>> };
    >>>>
    >>>> matrix = std::move(temp); // matrix = private class member
    >>>
    >>> Use simply 'matrix = temp;' here.
    >>>
    >>>> }
    >>>

    >>
    >> is there a better container for this? does std::vector can move its content?

    >
    >
    > 'std::valarray<float>' is better designed for doing mathematical
    > operations than vector or array but I would go farther and use some
    > linear algebra library instead.
    >

    thanks a lot

    cheers, chris
    Chris Forone, Mar 28, 2013
    #6
  7. Chris Forone

    Guest

    On Thursday, March 28, 2013 9:12:50 AM UTC, Chris Forone wrote:
    > does the std::move() assignement make sense here or is it used only in
    >
    > ctors?
    >


    std::move can be useful in operator= functions and
    other places. Here's an example of some marshalling
    code that uses it:

    template <class R>
    void
    Receive :):cmw::ReceiveBufferFile<R>& buf
    ,std::vector<std::deque<double> >& az1
    )
    {
    uint32_t count[2];
    count[0]=buf.template Give<uint32_t>();
    az1.reserve(az1.size()+count[0]);
    for(;count[0]>0;--count[0]){
    std::deque<double> rep2;
    count[1]=buf.template Give<uint32_t>();
    for(;count[1]>0;--count[1]){
    rep2.emplace_back(buf.template Give<double>());
    }
    az1.emplace_back:):std::move(rep2));
    }
    }


    The serialization library in Boost doesn't use
    std::move in this way --
    http://webEbenezer.net/comparison.html
    ..

    Brian
    Ebenezer Enterprises - so far G-d has helped us.
    http://webEbenezer.net
    , Mar 29, 2013
    #7
    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. Renate

    JBuilder mit Methode

    Renate, Aug 12, 2003, in forum: Java
    Replies:
    0
    Views:
    459
    Renate
    Aug 12, 2003
  2. =?Utf-8?B?bWlsbXVzIHRlbmRlcg==?=

    Override Render Methode - include in the control tag

    =?Utf-8?B?bWlsbXVzIHRlbmRlcg==?=, Jul 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    416
    =?Utf-8?B?bWlsbXVzIHRlbmRlcg==?=
    Jul 13, 2006
  3. VK
    Replies:
    11
    Views:
    508
    Aaron Bingham
    May 27, 2005
  4. Thomas Kowalski

    Passing array to methode

    Thomas Kowalski, Sep 19, 2006, in forum: C++
    Replies:
    5
    Views:
    286
  5. Jens Henrik Goebbert

    initialising static member with static methode

    Jens Henrik Goebbert, Oct 24, 2007, in forum: C++
    Replies:
    3
    Views:
    954
Loading...

Share This Page