By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,004 Members | 2,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,004 IT Pros & Developers. It's quick & easy.

valarray assignment question

P: n/a
I have a question about assignment of slices of valarrays. I want to
assign one general slice of a valarray to another. It seems the only
way is to use the static_cast syntax, but this seems to involve
needless copies.

Example code follows:

#include <iostream>
#include <valarray>

// Simple class to see what copying is occurring.
class Cl {
Cl() { std::cout << "Cl()" << std::endl; }
Cl(const Cl&) { std::cout << "Cl(const Cl&)" << std::endl; }
Cl& operator=(const Cl&) { std::cout << "operator=(const Cl&)" <<
std::endl; return *this; }

int main() {
std::valarray<Cl> u(4);
std::cout << "---" << std::endl;

std::valarray<Cl> v(4);
std::cout << "---" << std::endl;

std::gslice g(0, std::valarray<size_t>(2, 1),
std::valarray<size_t>(2, 1));
u[g] = static_cast<std::valarray<Cl> >(v[g]);

The output is:

operator=(const Cl&)
operator=(const Cl&)
operator=(const Cl&)
operator=(const Cl&)

i.e., there are 4 assignments instead of only 2. This is when using
-O3 optimisation on my compiler. Is there some way to avoid these
extra assignments?

Thank you.
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.