std::move in methode

C

Chris Forone

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
 
Ö

Öö Tiib

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.
 
C

Chris Forone

Am 28.03.2013 10:41, schrieb Öö Tiib:
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.


Use simply 'matrix = temp;' here.
is there a better container for this? does std::vector can move its content?
 
S

SG

Am 28.03.2013 10:41, schrieb Öö Tiib:



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
 
Ö

Öö Tiib

Am 28.03.2013 10:41, schrieb Öö Tiib:

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.
 
C

Chris Forone

Am 28.03.2013 12:36, schrieb Öö Tiib:
'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
 
W

woodbrian77

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
 

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

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top